array_intersect_key
(PHP 5> = 5.1.0)
array_intersect_key - Computes the intersection of array by comparing by keys
description
array array_intersect_key ( array $ array1 , array $ array2 [, array $ ... ])
array_intersect_key () returns an array containing all the values of array1 that have the same keys present in all arguments.
Parameters
- array1
- The array with keys to be checked.
- array2
- An array to compare keys.
- array
- A variable list of arrays for comparison.
Returned Value
Returns an associative array containing all the values of array1 that are present in all arguments.
Examples
Example # 1 Example of array_intersect_key ()
<?php
$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4);$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);var_dump(array_intersect_key($array1, $array2));?>
The above example will print:
array (2) { ["blue"] => int (1) ["green"] => int (3) }
In our example you can see that only the 'blue' and 'green' keys are present in both array and so returned. Also note that the values of the 'blue' and 'green' keys differ in the two arrays. The combination occurs because only the keys are checked. The returned values are from array1 .
The two keys of the key => value pair are considered equal only if (string) $ key1 === (string) $ key2 . In other words a type check is executed, then the string representation must be the same.
0 comments:
Post a Comment