PHP array_intersect_uassoc Function
last modified March 13, 2025
The PHP array_intersect_uassoc
function computes the intersection
of arrays with additional index check. It compares keys using a callback.
Basic Definition
array_intersect_uassoc
returns an array containing all values
from the first array that are present in all other arrays. Keys are compared
using a user-supplied callback function.
Syntax: array_intersect_uassoc(array $array1, array $array2, ..., callable $key_compare_func): array
.
The callback should return an integer less than, equal to, or greater than zero.
Basic array_intersect_uassoc Example
This example shows how to find intersection of arrays with case-insensitive key comparison.
<?php declare(strict_types=1); $array1 = ["a" => "apple", "B" => "banana", "c" => "cherry"]; $array2 = ["A" => "apple", "b" => "banana", "C" => "cherry"]; $result = array_intersect_uassoc($array1, $array2, function($a, $b) { return strcasecmp($a, $b); }); print_r($result);
This finds elements present in both arrays where keys match case-insensitively.
The callback uses strcasecmp
for case-insensitive comparison.
Numeric Key Comparison
Compare numeric keys with custom logic to find matching elements.
<?php declare(strict_types=1); $array1 = [1 => "one", 2 => "two", 3 => "three"]; $array2 = ["1" => "one", "2" => "two", 4 => "four"]; $result = array_intersect_uassoc($array1, $array2, function($a, $b) { return $a <=> $b; }); print_r($result);
This compares numeric keys loosely (string vs integer). The spaceship operator
(<=>
) handles the comparison, matching keys 1 and 2.
Complex Key Comparison
Use a more complex callback function to compare composite keys.
<?php declare(strict_types=1); $array1 = ["user_1" => "Alice", "user_2" => "Bob", "admin_1" => "Charlie"]; $array2 = ["USER_1" => "Alice", "user_3" => "Dave", "ADMIN_1" => "Charlie"]; $result = array_intersect_uassoc($array1, $array2, function($a, $b) { $aParts = explode('_', strtolower($a)); $bParts = explode('_', strtolower($b)); if ($aParts[0] !== $bParts[0]) return $aParts[0] <=> $bParts[0]; return $aParts[1] <=> $bParts[1]; }); print_r($result);
This compares keys by splitting them into parts and comparing each part. The callback normalizes case and compares type then ID separately.
Multiple Array Comparison
Compare more than two arrays with custom key comparison logic.
<?php declare(strict_types=1); $array1 = ["a" => "apple", "b" => "banana", "c" => "cherry"]; $array2 = ["A" => "apple", "B" => "banana", "D" => "date"]; $array3 = ["a" => "apple", "b" => "blueberry", "c" => "cherry"]; $result = array_intersect_uassoc($array1, $array2, $array3, function($a, $b) { return strcasecmp($a, $b); }); print_r($result);
This finds elements present in all three arrays with case-insensitive key matching. Only "apple" with key "a" appears in all arrays.
Object Key Comparison
Compare arrays with object keys using a custom comparison function.
<?php declare(strict_types=1); class User { public function __construct(public int $id) {} } $user1 = new User(1); $user2 = new User(2); $user3 = new User(1); $array1 = [$user1 => "Alice", $user2 => "Bob"]; $array2 = [$user3 => "Alice", $user2 => "Charlie"]; $result = array_intersect_uassoc($array1, $array2, function($a, $b) { return $a->id <=> $b->id; }); print_r($result);
This compares objects as keys by their ID property. The callback uses the spaceship operator to compare IDs, matching User 1 in both arrays.
Best Practices
- Consistent Callbacks: Ensure your callback provides consistent comparisons.
- Type Safety: Add type hints to callback parameters when possible.
- Performance: Keep key comparison logic efficient for large arrays.
- Documentation: Clearly document custom comparison logic.
Source
PHP array_intersect_uassoc Documentation
This tutorial covered the PHP array_intersect_uassoc
function with
practical examples showing its usage for array intersection with custom key comparison.
Author
List all PHP Array Functions.