Difference and Intersection

The difference functions are:
array_diff($arr1,$arr2[,$arr3……])
array_diff_ key($arr1,$arr2[,$arr3……])
array_diff_ukey($arr1, $arr2 [,$arr3……], $cf($m1,$m2))
array_diff_assoc($arr1,$arr2[,$arr3……])
array_diff_uassoc($arr1, $arr2 [,$arr3……], $cf($m1,$m2))
array_udiff($arr1, $arr2 [,$arr3……], $cf($m1,$m2))
array_udiff_assoc($arr1, $arr2 [,$arr3……], $cf($m1,$m2))
array_udiff_uassoc($arr1, $arr2 [,$arr3……], $cf($m1,$m2), $cf2($m3,$m4))
array_diff() returns a copy of  $arr1, removing entries with values repeated in the subsequent arrays. array_diff_key() is similar, but compares the keys instead. ‘assoc’ requires the keys to be repeated too for an entry to be removed. ‘u’ requires a comparison function to be supplied. The comparison function $cf has to return an integer < 0 if the first argument is less than the second, 0 if the first argument equals the second, > 1 if the first argument is greater than the second.

The intersection functions are:
array_intersect($arr1,$arr2[,$arr3……])
array_intersect_ key($arr1,$arr2[,$arr3……])
array_intersect_ukey($arr1, $arr2 [,$arr3……], $cf($m1,$m2))
array_intersect_assoc($arr1,$arr2[,$arr3……])
array_intersect_uassoc($arr1, $arr2
[,$arr3……],  $cf($m1,$m2))
array_uintersect($arr1, $arr2 [,$arr3……], $cf($m1,$m2))
array_uintersect_assoc($arr1, $arr2 
[,$arr3……], $cf($m1,$m2))
array_uintersect_uassoc($arr1, $arr2
[,$arr3……], $cf($m1,$m2), $cf2($m3,$m4))
While the difference functions remove repeated elements, these intersection functions return only the repeated elements.

<!DOCTYPE html><html><head></head>
<body>
<?php
$a=['a'=>'apple','b'=>'banana','c'=>'carrot','d'=>'pear'];
$b=['a'=>'apple','b'=>'orange','carrot'];
print_r(array_diff($a,$b)); echo "<br />";
print_r(array_diff_key($a,$b)); echo "<br />";
print_r(array_diff_assoc($a,$b)); echo "<br />";
print_r(array_intersect($a,$b)); echo "<br />";
print_r(array_intersect_key($a,$b)); echo "<br />";
print_r(array_intersect_assoc($a,$b)); echo "<br />";
?> 
</body></html>

Array ( [b] => banana [d] => pear ) 
Array ( [c] => carrot [d] => pear ) 
Array ( [b] => banana [c] => carrot [d] => pear ) 
Array ( [a] => apple [c] => carrot ) 
Array ( [a] => apple [b] => banana ) 
Array ( [a] => apple )