Arrays7 min readExamples run on PHP 8.5.10

array_intersect_uassoc()

Learn how array_intersect_uassoc() keeps entries whose values match and whose keys are judged equal by your own comparison callback, with examples and pitfalls.

Signature

array_intersect_uassoc(array $array, array ...$arrays, callable $key_compare_func): array

Purpose

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. Returning 0 means 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.

Example
<?php
$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;
Output
{"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.

Example
<?php
$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;
Output
{"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.

Example
<?php
$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;
Output
{"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" and true all 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

  1. Returning a boolean or a float from the callback. A callback that returns true/false (as with ==) gives the wrong meaning, since only 0 signals equality. Fractional floats such as 0.5 are cast to integer 0 and so read as equal. Use <=> or strcmp-style functions.
  2. Expecting values to use the callback. The callback only judges keys. If you need a custom value comparison, look at array_uintersect_assoc(), or at array_uintersect_uassoc() for both.
  3. Forgetting the callback position. Placing it first causes a TypeError, because it must be the final argument.
  4. Assuming keys are reindexed. Numeric keys from the first array are kept as they were, so the result may have gaps.
  • array_intersect() compares values only.
  • array_intersect_assoc() compares values and keys strictly, without a callback.
  • array_intersect_key() and array_intersect_ukey() compare keys only.
  • array_uintersect_assoc() and array_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

Case-insensitive key comparison with strcasecmp
<?php
$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;
Output
{"b":"brown"}
Intersecting three arrays with case-insensitive keys
<?php
$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;
Output
{"Host":"localhost","Port":"3306"}
Custom callback treating keys in the same bucket of ten as equal
<?php
$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;
Output
{"11":"x","31":"z"}