Understanding the PHP Array Intersect Function with uassoc
In this article, we will dive into the world of PHP programming and explore a powerful function known as array_intersect_uassoc. This function is used to
array_intersect_uassoc() computes the intersection of two or more arrays — it returns the entries from the first array whose value AND key also appear in every other array. What makes this variant special is the u in its name: you supply a user-defined callback that decides when two keys are considered equal. Values are still compared with PHP's built-in (loose, string-cast) comparison.
This page explains how the function works, walks through a verified example, and shows when to reach for it instead of the other array_intersect_* variants.
Syntax
array_intersect_uassoc(
array $array,
array ...$arrays,
callable $key_compare_func
): array$array— the array to compare against; its entries appear in the result....$arrays— one or more arrays to compare against the first.$key_compare_func— a callback that compares two keys. It must return an integer less than, equal to, or greater than0when the first key is considered less than, equal to, or greater than the second (the same contract as ausortcomparator).
The function returns a new array containing the matching entries from $array, preserving its original keys.
How the key callback works
Unlike array_intersect_assoc(), which compares keys with ===, this function delegates key comparison to your callback. That lets you implement matching rules PHP does not offer out of the box — case-insensitive keys, trimmed keys, or locale-aware ordering.
Note the split responsibility: keys are compared by your callback, but values are still compared by the function itself (loosely, as strings). If you also need a custom value comparison, use array_uintersect_uassoc().
Example
<?php
// The callback receives two keys and must return an int:
// 0 = equal, -1 = first is smaller, 1 = first is larger.
function compareKeys($key1, $key2) {
if ($key1 === $key2) {
return 0;
}
return ($key1 > $key2) ? 1 : -1;
}
$array1 = ["a" => "green", "b" => "brown", "c" => "blue", "red"];
$array2 = ["a" => "green", "yellow", "red"];
$result = array_intersect_uassoc($array1, $array2, "compareKeys");
print_r($result);
?>Output:
Array
(
[a] => green
)Only "a" => "green" survives, because it is the single entry whose key and value both appear in $array2:
"b" => "brown"and"c" => "blue"— keys don't exist in$array2.0 => "red"in$array1— the value"red"exists in$array2, but at key1, so the keys (0vs1) don't match.
When to use it
Reach for array_intersect_uassoc() when you need a key-aware intersection and the keys require custom matching logic. A common case is case-insensitive header or column matching:
<?php
function ci_keys($k1, $k2) {
return strcasecmp((string) $k1, (string) $k2);
}
$config = ["Host" => "localhost", "Port" => 8080];
$expected = ["host" => "localhost", "PORT" => 8080];
print_r(array_intersect_uassoc($config, $expected, "ci_keys"));
?>This keeps Host => localhost and Port => 8080 because the callback treats Host/host and Port/PORT as the same key, while the values still match.
Related functions
| Function | Key comparison | Value comparison |
|---|---|---|
array_intersect() | ignored | built-in |
array_intersect_assoc() | === | built-in |
array_intersect_key() | === (keys only) | ignored |
array_intersect_ukey() | callback (keys only) | ignored |
array_intersect_uassoc() | callback | built-in |
array_uintersect_uassoc() | callback | callback |
Conclusion
array_intersect_uassoc() returns the entries of the first array whose value and key both appear in every other array, while letting you control key matching through a user-defined callback. Use it when an associative intersection needs flexible key rules — such as case-insensitive or trimmed keys — and remember that values are still compared with PHP's built-in comparison.