[PHP 5 >= 5.1.0, PHP 7, PHP 8]
array_intersect_key — Computes the intersection of arrays using keys for comparison
Description
array_intersect_key[array $array
, array ...$arrays
]: array
Parameters
array
The array with master keys to check.
arrays
Arrays to compare keys against.
Return Values
Returns an associative array containing all the entries of array
which have keys that are present in all arguments.
Changelog
8.0.0 | This function can now be called with only one parameter. Formerly, at least two parameters have been required. |
Examples
Example #1 array_intersect_key[] example
The above example will output:
array[2] { ["blue"]=> int[1] ["green"]=> int[3] }
In our example you see that only the keys 'blue'
and 'green'
are present in both arrays and thus returned. Also notice that the values for the keys 'blue'
and 'green'
differ
between the two arrays. A match still occurs because only the keys are checked. The values returned are those of array
.
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 the same.
See Also
- array_diff[] - Computes the difference of arrays
- array_udiff[] - Computes the difference of arrays by using a callback function for data comparison
- array_diff_assoc[] - Computes the difference of arrays with additional index check
- array_diff_uassoc[] - Computes the difference of arrays with additional index check which is performed by a user supplied callback function
- array_udiff_assoc[] - Computes the difference of arrays with additional index check, compares data by a callback function
- array_udiff_uassoc[] - Computes the difference of arrays with additional index check, compares data and indexes by a callback function
- array_diff_key[] - Computes the difference of arrays using keys for comparison
- array_diff_ukey[] - Computes the difference of arrays using a callback function on the keys for comparison
- array_intersect[] - Computes the intersection of arrays
- array_intersect_assoc[] - Computes the intersection of arrays with additional index check
- array_intersect_uassoc[] - Computes the intersection of arrays with additional index check, compares indexes by a callback function
- array_intersect_ukey[] - Computes the intersection of arrays using a callback function on the keys for comparison
vladas dot dirzys at gmail dot com ¶
10 years ago
Simple key white-list filter:
Will return:
Array
[
[b] => 213
[c] => 321
]
Anonymous ¶
1 year ago
Note that the order of the keys in the returned array is the same as the order of the keys in the source array.
To sort by the second array, then you may do so through array_replace.
Shows:
Array
[
[one] => c
[three] => b
]
Rather than:
Array
[
[three] => b
[one] => c
]
github.com/xmarcos ¶
8 years ago
[Editor's note: changed array_merge_recursive[] to array_replace_recursive[] to fix the script]
Here is a better way to merge settings using some defaults as a whitelist.
array [size=4]
Output:
'id' => int 123456
'client_id' => int 789
'client_secret' => string '5ebe2294ecd0e0f08eab7690d2a6ee69' [length=32]
'options' =>
array [size=2]
'trusted' => boolean false
'active' => boolean true
Reed Silver ¶
7 years ago
If you want an array that has no key value pairs added from the second array:
$new = array_intersect_key[$b, $a] + $a;
pgl at yoyo dot org ¶
11 years ago
Note that the order of the keys in the returned array is the same as the order of the keys in the source array. eg:
Shows:
Array
[
[three] => b
[one] => c
]
chrisbloom7 at gmail dot com ¶
12 years ago
Regarding php at keithtylerdotcom solution to emulate
His recommended solution will still return an array. To get the value of a single key in an array returned by a function, simply add implode[] to the recipe:
CBWhiz at gmail dot com ¶
14 years ago
I have found the following helpful:
It's use is like both of the functions it uses, but keeps defaults and _only_ defaults. It's designed for key arrays, and i'm not sure how it will work on numeric indexed arrays.
Example:
Anton Backer ¶
16 years ago
Jesse: no, array_intersect_key does not accomplish the same thing as what you posted:
array_flip [array_intersect [array_flip [$a], array_flip [$b]]]
because when the array is flipped, values become keys. having duplicate values is not a problem, but having duplicate keys is. array_flip resolves it by keeping only one of the duplicates and discarding the rest. by the time you start intersecting, you've already lost information.
markus dot kappe at dix dot at ¶
12 years ago
pixelf3hler at visualize-me dot de ¶
8 years ago
in case you came here looking for a function that returns an array containing the values of `all` arrays with intersecting keys:
pdemaziere at gmail dot com ¶
13 years ago
Just a simple script if you want to use one array, which contains only zeros and ones, as mask for another one [both arrays must have the same size of course]. $outcome is an array that contains only those values from $source where $mask is equal to 1.
PS: the array_values[] function is necessary to ensure that both arrays have the same numbering/keys, otherwise your masking does not behave as you expect.
Enjoy!