Array Intersect
<?php
$array1 = array(2, 4, 6, 8, 10, 12);
$array2 = array(1, 2, 3, 4, 5, 6);
echo "<pre>";
print_r(array_intersect($array1, $array2));
echo "</pre>";
echo "<pre>";
print_r(array_intersect($array2, $array1));
echo "</pre>";
?>
// output
Array
(
[0] => 2
[1] => 4
[2] => 6
)
Array
(
[1] => 2
[3] => 4
[5] => 6
)
Array Union
<?php
$a = array(1,2,3,4);
$b = array(2,4,5,6);
// $a = 1 2 3 4
$union = // $b = 2 4 5 6
array_merge(
array_intersect($a, $b), // 2 4
array_diff($a, $b), // 1 3
array_diff($b, $a) // 5 6
); // $u = 1 2 3 4 5 6
?>
// Output
Array
(
[0] => 2
[1] => 4
[2] => 1
[3] => 3
[4] => 5
[5] => 6
)
0 Comments