The Note You're Voting On
ivan dot jelenic42 at gmail dot com ¶ 4 months ago
It preserves from the first array:
- keys
- order of elements
- duplicates
Duplicates: note that if the first array has just one value, and the other array has two of those values, only one will be returned, but in the opposite case, two will be returned.
Order of elements: it's preserved from the first array even if integer keys are out of order.
Example:
________________________________
<?php
$array1 = ['a' => 'cherry', 'b' => 'banana', 'c' => 'cherry'];
$array2 = [1 => 'banana', 0 => 'cherry', 2 => 'date'];
$result1 = array_intersect($array1, $array2);
$result2 = array_intersect($array2, $array1);
echo "Result from array_intersect(\$array1, \$array2):\n";
print_r($result1);
echo "\nResult from array_intersect(\$array2, \$array1):\n";
print_r($result2);
?>
___________________________________________
Outputs:
Result from array_intersect($array1, $array2):
Array
(
[a] => cherry
[b] => banana
[c] => cherry
)
Result from array_intersect($array2, $array1):
Array
(
[1] => banana
[0] => cherry
)