array_intersect_ukey
Keeps the entries whose keys exist in every array, using a callback to decide when two keys are equal.
array_intersect_ukey(array $array, array ...$arrays, callable $key_compare_func): array- Added in PHP 5.1.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The initial array to compare from. |
$arraysoptional | array | One or more arrays whose keys are compared against. |
$key_compare_func | callable | Comparison callback of the form fn(mixed $a, mixed $b): int, returning a negative number, zero or a positive number when $a is less than, equal to or greater than $b. |
Return value
arrayAn array of the entries of $array whose keys the callback considers present in all of the other arrays.
Errors
Passing a non-callable last argument, or a non-array in any earlier position, throws a TypeError.
array_intersect_ukey() is array_intersect_key() with a pluggable notion of key equality. Values are ignored completely — they are only carried through from the first array — so the callback is the entire matching logic.
The comparator contract
The callback receives two keys and must return an integer: negative if the first sorts before the second, 0 if they are equal, positive otherwise. PHP does not just call it for every pair; it sorts the keys using your comparator and then walks them, so the function must be a consistent ordering. A comparator that says "equal" for unrelated keys, or that is not transitive, produces results that look random.
When it earns its place
Reach for it when the keys on both sides mean the same thing but are written differently: different letter case, a prefix on one side, snake_case against camelCase. For anything simpler, normalising the keys once with array_combine() and array_map() and then using array_intersect_key() is usually faster and easier to read.
Callback position
As with all the u variants, the callable is the last argument, after every array.
Example
When to use it
- Intersect two arrays whose keys differ only in case, such as HTTP header names.
- Match config keys written in snake_case against ones written in camelCase.
- Line up arrays keyed by version strings using version_compare as the comparator.
Frequently asked questions
What is the difference between array_intersect_ukey and array_intersect_uassoc?
array_intersect_ukey compares keys only and ignores values entirely, while array_intersect_uassoc requires the values to match as well. Both take the key comparator as the last argument.
Can the callback just return true or false?
No. It must return an integer ordering, because false casts to 0 which means "equal". Use the <=> operator or strcasecmp() so the comparator is a real three-way comparison.
Which array's keys and values end up in the result?
Always the first array's. The later arrays only contribute their keys to the matching, and their values are never read.
More examples
Case-insensitive header lookup
HTTP header names are case-insensitive, so strcasecmp matches Content-Type to content-type. The result keeps the original casing from $headers, which array_key_first confirms.
<?php
$headers = ['Content-Type' => 'text/html', 'X-Trace' => 'abc', 'Accept' => '*/*'];
$wanted = ['content-type' => 1, 'accept' => 1];
$found = array_intersect_ukey($headers, $wanted, 'strcasecmp');
print_r($found);
echo array_key_first($found), "\n";Matching snake_case to camelCase
Both keys are stripped of underscores and lowercased before comparison, so user_id matches userId. deleted_at has no counterpart and is dropped.
<?php
$db = ['user_id' => 7, 'first_name' => 'Ada', 'deleted_at' => null];
$api = ['userId' => 1, 'firstName' => 1];
$norm = fn($k) => strtolower(str_replace('_', '', (string) $k));
$cmp = fn($a, $b) => $norm($a) <=> $norm($b);
print_r(array_intersect_ukey($db, $api, $cmp));
Discussion