You may use array_intersect()
for performing the operation that you need.
array_intersect()
- Computes the intersection of arrays
array_intersect()
returns an array containing all the values of array1 that are present in all the arguments. Note that keys are preserved.
The array_intersect()
function compares the values of two (or more) arrays, and returns the matches.
This function compares the values of two or more arrays, and return an array that contains the entries from array1 that are present in array2, array3, etc.
Return Value: Returns an array containing the entries from array1
that are present in all of the other arrays.
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");
$result=array_intersect($a1,$a2);
print_r($result);
?>
Output:
Array ( [a] => red [b] => green [c] => blue )
Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same.