array_intersect_uassoc

Intersects arrays on key and value, comparing the keys with a callback you supply.

Signature
array_intersect_uassoc(array $array, array ...$arrays, callable $key_compare_func): array
  • Added in PHP 5.0

Parameters

ParameterTypeDescription
$arrayarrayThe initial array to compare from.
$arraysoptionalarrayOne or more arrays to compare against.
$key_compare_funccallableComparison 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 values are present in all arguments and whose keys the callback considers equal.

Errors

Passing a non-callable last argument, or a non-array in any earlier position, throws a TypeError.

array_intersect_uassoc() works like array_intersect_assoc() — an entry needs a matching key and a matching value — except that you decide what "matching key" means. The callback is a three-way comparator, the same shape usort() uses: return 0 for equal, and any negative or positive integer otherwise.

Only the keys go through the callback

This trips people up regularly. Values are still compared internally as strings; only key equality is delegated to your function. If you need custom value comparison, the function you want is array_uintersect_assoc(), and if you need both, array_uintersect_uassoc().

Argument order

The callback is always the last argument, after every array. That makes the signature variadic-with-a-tail, which is unusual in PHP and means you cannot simply spread an array of arrays into the call without appending the callback afterwards.

Return an int, not a bool

Returning true/false from the comparator is a classic bug: false casts to 0, which means "equal", so everything matches. Floats are also cast to int, so 0.5 becomes 0 and reads as equal. Use <=> or strcasecmp().

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Match two config arrays whose keys differ only in letter case.
  • Intersect records keyed by ids that need normalising (trimmed, lowercased) before comparison.
  • Compare arrays keyed by version strings using version_compare as the comparator.

Frequently asked questions

Does the callback in array_intersect_uassoc compare keys or values?

Keys only. Values are still compared by their string representation, so use array_uintersect_assoc() if you need custom value comparison.

Where does the callback go in the argument list?

Always last, after every array argument. If you build the arguments dynamically, append the callable before spreading them into the call.

Why does my comparator match everything?

It is probably returning a bool or a float. false casts to 0 and 0.5 truncates to 0, and 0 means "equal" — return a real integer, for example with the <=> operator.

More examples

Normalising keys before comparing

The comparator trims and lowercases both keys, so ' sku-1 ' matches 'SKU-1' and the pen entry survives. sku-2 has matching keys but different values, so it is dropped.

Example · php
<?php
$a = ['  sku-1 ' => 'pen', 'sku-2' => 'book'];
$b = ['SKU-1' => 'pen', 'sku-2' => 'lamp'];

$cmp = fn($x, $y) => strcasecmp(trim((string) $x), trim((string) $y));

print_r(array_intersect_uassoc($a, $b, $cmp));

Spreading arrays plus the callback

Because the callback must come last, the arrays and the comparator are merged into one argument list before spreading. Only A => 1 matches key and value in every array.

Example · php
<?php
$base = ['A' => 1, 'B' => 2, 'C' => 3];
$others = [['a' => 1, 'b' => 9], ['A' => 1, 'b' => 2]];

$args = array_merge([$base], $others, ['strcasecmp']);
print_r(array_intersect_uassoc(...$args));

Discussion

  • Be the first to comment on this lesson.