array_intersect_key() returns an array containing all the values of array1 which have matching keys that are present in all the arguments.
The array with master keys to check.
An array to compare keys against.
A variable list of arrays to compare.
Returns an associative array containing all the values of array1 which have matching keys that are present in all arguments.
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);
$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);
The above example will output:
In our example you see that only the keys
'green' are present in both arrays and thus
returned. Also notice that the values for the keys
'green' differ between
the two arrays. A match still occurs because only the keys are checked.
The values returned are those of array1.
The two keys from the
key => value pairs are
considered equal only if
(string) $key1 === (string) $key2 . In other words
a strict type check is executed so the string representation must be