PHP/Data Structure/array intersect — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 10:37, 26 мая 2010
Содержание
array_intersect
<?php
$array1 = array("OH","CA","NY","HI","CT");
$array2 = array("OH","CA","HI","NY","IA");
$array3 = array("TX","MD","NE","OH","HI");
$intersection = array_intersect($array1, $array2, $array3);
print_r($intersection);
?>
array_intersect_assoc
<?php
$array1 = array("OH" => "Ohio", "CA" => "California", "HI" => "Hawaii");
$array2 = array("50" => "Hawaii", "CA" => "California", "OH" => "Ohio");
$array3 = array("TX" => "Texas", "MD" => "Maryland", "OH" => "Ohio");
$intersection = array_intersect_assoc($array1, $array2, $array3);
print_r($intersection);
?>
array_intersect( ) function returns a new array containing all the values of array $arr1 that exist in array $arr2.
array array_intersect ( array arr1, array arr2 [, array ...] )
<?
$toppings1 = array("Pepperoni", "Cheese", "Anchovies", "Tomatoes");
$toppings2 = array("Ham", "Cheese", "Peppers");
$int_toppings = array_intersect($toppings1, $toppings2);
var_dump($int_toppings);
?>
Array Intersection
<?php
$first = array("e", "h", "r", "j", "b");
$last = array("w", "e", "c");
$intersection = array_intersect($last, $first);
echo "<p>";
foreach ($intersection as $v) { echo "{$v} "; }
echo "</p>\n";
?>
Intersecting two arrays that have no duplicate keys, all the keys will be retained.
<?
$arr1 = array("Paul"=>25, "Ildiko"=>38, "Nick"=>27);
$arr2 = array("Ildiko"=>27, "Paul"=>38);
print "\nIntersect:\n";
var_dump(array_intersect($arr1, $arr2));
?>