PHP array_diff_uassoc function tutorial shows how to compute array difference with key comparison in PHP. Learn array_diff_uassoc with practical examples.
last modified March 13, 2025
The PHP array_diff_uassoc function computes the difference of arrays with additional index check. It compares both keys and values using a user-defined callback function.
The array_diff_uassoc function compares arrays and returns the difference. It checks both keys and values, using a callback for key comparison.
Syntax: array_diff_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 a simple comparison of two arrays with custom key comparison.
basic_array_diff_uassoc.php
<?php
function key_compare($a, $b) { if ($a === $b) { return 0; } return ($a > $b) ? 1 : -1; }
$array1 = [“a” => “red”, “b” => “green”, “c” => “blue”]; $array2 = [“a” => “red”, “d” => “green”];
$result = array_diff_uassoc($array1, $array2, “key_compare”);
print_r($result);
This compares two arrays using a custom key comparison function. The output shows elements from $array1 not present in $array2, considering both keys and values.
This example demonstrates case-insensitive key comparison between arrays.
case_insensitive.php
<?php
function case_insensitive_compare($a, $b) { return strcasecmp($a, $b); }
$array1 = [“A” => “apple”, “B” => “banana”, “C” => “cherry”]; $array2 = [“a” => “apple”, “b” => “berry”];
$result = array_diff_uassoc($array1, $array2, “case_insensitive_compare”);
print_r($result);
The callback uses strcasecmp for case-insensitive comparison. Only elements with different case-insensitive keys or different values are returned in the result.
This example shows how to compare more than two arrays using array_diff_uassoc.
multiple_arrays.php
<?php
function numeric_compare($a, $b) { return $a <=> $b; }
$array1 = [10 => “ten”, 20 => “twenty”, 30 => “thirty”]; $array2 = [10 => “ten”, 20 => “twentyone”]; $array3 = [10 => “ten”, 30 => “thirtytwo”];
$result = array_diff_uassoc($array1, $array2, $array3, “numeric_compare”);
print_r($result);
The function compares all arrays against the first array. Only elements not present in any of the other arrays (considering both keys and values) are returned. The spaceship operator simplifies the comparison.
This example demonstrates a more complex key comparison logic.
complex_comparison.php
<?php
function complex_compare($a, $b) { // Compare string length first, then alphabetical order $len1 = strlen($a); $len2 = strlen($b);
if ($len1 != $len2) {
return $len1 <=> $len2;
}
return strcmp($a, $b);
}
$array1 = [“apple” => 1, “banana” => 2, “cherry” => 3]; $array2 = [“apple” => 1, “berry” => 4, “cherry” => 5];
$result = array_diff_uassoc($array1, $array2, “complex_compare”);
print_r($result);
The callback first compares key lengths, then alphabetical order. This shows how to implement multi-criteria comparison logic for array keys.
This advanced example compares arrays with object keys using array_diff_uassoc.
object_keys.php
<?php
class ProductKey { public function __construct(public string $id) {} }
function object_compare($a, $b) { return strcmp($a->id, $b->id); }
$key1 = new ProductKey(“p1”); $key2 = new ProductKey(“p2”); $key3 = new ProductKey(“p3”);
$array1 = [$key1 => “Laptop”, $key2 => “Phone”]; $array2 = [$key1 => “Laptop”, $key3 => “Tablet”];
$result = array_diff_uassoc($array1, $array2, “object_compare”);
print_r($result);
This demonstrates comparing arrays with object keys. The callback compares objects based on their id property. Only the Phone entry is returned as it’s not present in $array2 with the same key and value.
Consistent Callbacks: Ensure your comparison function is consistent.
Performance: Keep comparison logic efficient for large arrays.
Type Safety: Add type hints in PHP 7+ for robust code.
Documentation: Clearly document your comparison logic.
PHP array_diff_uassoc Documentation
This tutorial covered the PHP array_diff_uassoc function with practical examples showing its usage for array comparison scenarios.
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.