A Comprehensive Guide to PHP array_uintersect_assoc Function
Learn how PHP array_uintersect_assoc() computes the intersection of arrays with an index check and a user-defined value comparison callback.
array_uintersect_assoc() computes the intersection of two or more arrays with an additional index check, where the values are compared by a callback you supply but the keys are compared with the built-in (loose) comparison. It returns the entries of the first array whose key and value also appear in every other array.
The "u" in the name means user-defined value comparison; the _assoc suffix means keys are checked too. So an element survives only when both of these are true:
- its key exists in all the other arrays (compared by PHP's standard comparison), and
- your callback reports its value as equal to the matching element in every other array.
This makes it the strict cousin of array_intersect(), which ignores keys and uses string comparison. Use array_uintersect_assoc() when matching keys matters and you need custom value matching — for example case-insensitive matching, comparing objects, or matching numbers with a tolerance.
Syntax
array_uintersect_assoc(array $array1, array $array2, ...$arrays, callable $value_compare_func): arrayThe callback is always the last argument. Everything before it is an array to intersect.
Parameters
| Parameter | Description |
|---|---|
$array1 | The base array. Keys and values from this array are preserved in the result. |
$array2, ... | One or more arrays to compare against $array1. |
$value_compare_func | A callback that compares two values and returns an integer. |
Return value
An associative array containing every entry of $array1 whose key is present in all the other arrays and whose value the callback considers equal across all of them. Keys are preserved from $array1.
The callback function
The callback receives two values and must return an integer that orders them, exactly like the comparison functions used by usort():
0when the two values are considered equal,- a negative number (
< 0) when the first is "less than" the second, - a positive number (
> 0) when the first is "greater than" the second.
Two values are treated as a match only when the callback returns 0. Built-in functions such as strcasecmp() (case-insensitive string compare) and strcmp() already follow this contract, which is why they can be passed directly.
Examples
Example 1: Basic intersection across three arrays
Output:
Array
(
[c] => cherry
)Only c => cherry survives. apple is in $array1 and $array3 but missing from $array2; banana is in $array1 and $array2 but missing from $array3. Only cherry appears under the same key c in all three arrays, so it is the only match. Note that the key c is preserved from $array1.
Example 2: Case-insensitive value matching
Output:
Array
(
[c] => Cherry
)The values differ in case across the arrays (Cherry vs cherry), but because strcasecmp compares case-insensitively, the callback still reports them as equal. The result keeps the value as it appears in $array1 (Cherry).
Example 3: Why the index check matters
The "assoc" part is easiest to see when the same value sits under different keys:
<?php
$array1 = array('x' => 'red', 'y' => 'green', 'z' => 'blue');
$array2 = array('x' => 'red', 'w' => 'green', 'z' => 'BLUE');
$result = array_uintersect_assoc($array1, $array2, "strcasecmp");
print_r($result);
?>Output:
Array
(
[x] => red
[z] => blue
)red matches because it shares key x in both arrays. blue/BLUE matches because they share key z and strcasecmp ignores case. But green is dropped: it is under key y in the first array and key w in the second, so the keys do not line up. If you used the plain array_uintersect() here, green would be included because keys would be ignored.
Related functions
array_intersect()— intersect by value only, using string comparison.array_intersect_assoc()— intersect by key and value, using string comparison (no callback).array_uintersect()— intersect by value with a callback, ignoring keys.array_uintersect_uassoc()— like this function, but the keys are compared with a second callback too.
Conclusion
array_uintersect_assoc() keeps the entries of the first array whose key appears in every other array and whose value the callback treats as equal across all of them. Reach for it when keys are meaningful and value matching needs custom logic. If you also need control over how keys are compared, use array_uintersect_uassoc(); if string value comparison is enough, the simpler array_intersect_assoc() does the job.