PHP array_intersect_uassoc function tutorial shows how to compute array intersection with key comparison in PHP. Learn array_intersect_uassoc with practical examples.
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.
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.
This example shows how to find intersection of arrays with case-insensitive key comparison.
basic_array_intersect_uassoc.php
<?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.
Compare numeric keys with custom logic to find matching elements.
numeric_key_comparison.php
<?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.
Use a more complex callback function to compare composite keys.
complex_key_comparison.php
<?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.
Compare more than two arrays with custom key comparison logic.
multiple_array_comparison.php
<?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.
Compare arrays with object keys using a custom comparison function.
object_key_comparison.php
<?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.
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.
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.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all PHP Array Functions.