Signature
array_intersect_uassoc(array $array, array ...$arrays, callable $key_compare_func): arrayPurpose
array_intersect_uassoc() finds the entries of the first array that also appear in every other array you pass. An entry counts as shared only when both its value and its key match. Values are compared as strings. Keys are compared by a callback that you supply, which lets you define what "the same key" means, for example ignoring letter case or treating nearby numbers as equal.
Use it when plain array_intersect_assoc() is too strict about keys, but you still want values to be matched exactly.
Signature
Parameters
$array: the array whose entries are candidates. The result is built from this array....$arrays: one or more arrays to compare against. An entry must be found in each of them.$key_compare_func: the last argument. It receives two keys and must return an integer that is less than, equal to, or greater than zero. Returning0means the keys are considered equal. The callback has no default; it is required.
Because the callback comes last, remember that every argument before it is treated as an array to intersect.
Return value and failure cases
The function returns an array containing the entries of the first array that are present in all the other arrays. Original keys of the first array are preserved, and the order follows the first array. If nothing matches, you get an empty array.
If the last argument is not a valid callable, or an earlier argument is not an array, PHP throws a TypeError. The function never modifies its inputs.
Example 1: case-insensitive keys
The built-in strcasecmp makes "b" and "B" equal. The key a fails because its values differ in case (green against GREEN), and value comparison is still exact. The numeric key 0 in the first array holds red, but in the second array red sits at key 1, so it does not match.
$array1 = ["a" => "green", "b" => "brown", "c" => "blue", "red"];
$array2 = ["a" => "GREEN", "B" => "brown", "yellow", "red"];
echo json_encode(array_intersect_uassoc($array1, $array2, "strcasecmp")), PHP_EOL;{"b":"brown"}
Example 2: several arrays at once
You can compare against more than two arrays. Here configuration names are matched without regard to case. Notice that the value "3306" matches the integer 3306, because values are compared by their string form. Mode is dropped because the last array has no matching key.
$defaults = ["Host" => "localhost", "Port" => "3306", "Mode" => "strict"];
$envOne = ["host" => "localhost", "PORT" => "3306", "mode" => "loose"];
$envTwo = ["HOST" => "localhost", "port" => 3306];
$result = array_intersect_uassoc($defaults, $envOne, $envTwo, "strcasecmp");
echo json_encode($result), PHP_EOL;{"Host":"localhost","Port":"3306"}
Example 3: a custom key rule
A callback can express any key equivalence. This one puts integer keys into buckets of ten, so 11 and 19 are "equal" while 25 and 20 are too. The entry at 25 is still removed because its value y differs from q. The surviving entries keep the keys from the first array.
$first = [11 => "x", 25 => "y", 31 => "z"];
$second = [19 => "x", 20 => "q", 39 => "z"];
$sameBucket = fn(int $a, int $b): int => intdiv($a, 10) <=> intdiv($b, 10);
echo json_encode(array_intersect_uassoc($first, $second, $sameBucket)), PHP_EOL;{"11":"x","31":"z"}
Edge cases
- Only the first array supplies the output. Keys and values from the other arrays never appear in the result.
- Values use string comparison.
1,"1"andtrueall become"1", so they count as equal. Nested arrays are not suitable values, since they cannot be converted to a meaningful string. - The callback should be consistent. It must behave like a proper ordering function, and should not depend on external state, because the order in which PHP calls it is not something to rely on.
- Empty input. An empty first array always gives an empty result.
Common mistakes
- Returning a boolean or a float from the callback. A callback that returns
true/false(as with==) gives the wrong meaning, since only0signals equality. Fractional floats such as0.5are cast to integer0and so read as equal. Use<=>orstrcmp-style functions. - Expecting values to use the callback. The callback only judges keys. If you need a custom value comparison, look at
array_uintersect_assoc(), or atarray_uintersect_uassoc()for both. - Forgetting the callback position. Placing it first causes a
TypeError, because it must be the final argument. - Assuming keys are reindexed. Numeric keys from the first array are kept as they were, so the result may have gaps.
Related functions
array_intersect()compares values only.array_intersect_assoc()compares values and keys strictly, without a callback.array_intersect_key()andarray_intersect_ukey()compare keys only.array_uintersect_assoc()andarray_uintersect_uassoc()supply callbacks for values, or for values and keys.
Recap
array_intersect_uassoc() returns the entries of the first array whose string-compared value is present in every other array under a key that your callback says is equal. Put the callback last, return 0 for equal keys, and remember that values never use the callback.
Tested examples
$array1 = ["a" => "green", "b" => "brown", "c" => "blue", "red"];
$array2 = ["a" => "GREEN", "B" => "brown", "yellow", "red"];
echo json_encode(array_intersect_uassoc($array1, $array2, "strcasecmp")), PHP_EOL;{"b":"brown"}
$defaults = ["Host" => "localhost", "Port" => "3306", "Mode" => "strict"];
$envOne = ["host" => "localhost", "PORT" => "3306", "mode" => "loose"];
$envTwo = ["HOST" => "localhost", "port" => 3306];
$result = array_intersect_uassoc($defaults, $envOne, $envTwo, "strcasecmp");
echo json_encode($result), PHP_EOL;{"Host":"localhost","Port":"3306"}
$first = [11 => "x", 25 => "y", 31 => "z"];
$second = [19 => "x", 20 => "q", 39 => "z"];
$sameBucket = fn(int $a, int $b): int => intdiv($a, 10) <=> intdiv($b, 10);
echo json_encode(array_intersect_uassoc($first, $second, $sameBucket)), PHP_EOL;{"11":"x","31":"z"}