Table of Content
PHP array_diff_uassoc compares arrays by values and keys. You can also pass a custom function to compare keys in your own way.
Understand the array_diff_uassoc Function in PHP
The function compares one array with another array. It checks both the values and the keys.
The syntax looks like this:
array_diff_uassoc($array1, $array2, callable $key_compare_func)$array1: the main array.$array2: the array to compare against.$key_compare_func: a function that compares the keys.
The function returns an array. This array contains the entries from the first array that are not present in the second array.
Here is an example:
function compareKeys($a, $b) {
if ($a === $b) return 0;
return ($a > $b) ? 1 : -1;
}
$a1 = ["x" => 10, "y" => 20, "z" => 30];
$a2 = ["x" => 10, "y" => 25, "w" => 30];
$result = array_diff_uassoc($a1, $a2, "compareKeys");
print_r($result);This code compares arrays by both values and keys. The result includes keys that do not match in the second array.
Here is the output:
Array
(
[y] => 20
[z] => 30
)
The function checks values and keys at the same time. Your custom function decides how the keys compare. The output shows the difference between arrays.
You use this function when you need a strict comparison between arrays. This includes cases where both keys and values must match exactly.
The custom function decides how the keys compare. You can define rules for equal, greater, or smaller. This gives you control over how the different result forms.
Examples of the array_diff_uassoc in PHP
Match Arrays with Same Keys:
function cmp($a, $b) {
return strcmp($a, $b);
}
$arr1 = ["id" => 1, "age" => 20, "name" => "Sam"];
$arr2 = ["id" => 1, "age" => 22, "name" => "Sam"];
$out = array_diff_uassoc($arr1, $arr2, "cmp");
print_r($out);
/*
Array
(
[age] => 20
)
*/This example compares arrays with string keys. The function detects that the values under the key "age" differ. The result contains "age" => 20 because that does not match the second array.
Compare with Different Keys:
function keyCheck($a, $b) {
return $a <=> $b;
}
$arr1 = ["a" => 100, "b" => 200, "c" => 300];
$arr2 = ["a" => 100, "b" => 200, "d" => 400];
$out = array_diff_uassoc($arr1, $arr2, "keyCheck");
print_r($out);This example compares arrays with numeric values but different keys. The function shows "c" => 300 because the second array has no key "c":
Array
(
[c] => 300
)
Custom Rule for Keys:
function myKeyRule($a, $b) {
if (strtolower($a) === strtolower($b)) return 0;
return ($a > $b) ? 1 : -1;
}
$arr1 = ["X" => 1, "Y" => 2, "Z" => 3];
$arr2 = ["x" => 1, "y" => 2, "W" => 3];
$out = array_diff_uassoc($arr1, $arr2, "myKeyRule");
print_r($out);This example compares keys in a case-insensitive way. The function ignores case when it checks the keys. The result only includes "Z" => 3 because the second array has no "Z" or "z".
The output:
Array
(
[Z] => 3
)
Numbers as Keys:
function compareNumKeys($a, $b) {
return $a - $b;
}
$arr1 = [1 => "one", 2 => "two", 3 => "three"];
$arr2 = [1 => "one", 4 => "two", 5 => "three"];
$out = array_diff_uassoc($arr1, $arr2, "compareNumKeys");
print_r($out);This example compares arrays with numbers as keys. The function detects that key 2 and 3 exist only in the first array. The result includes those unmatched pairs.
Here is the result:
Array
(
[2] => two
[3] => three
)
Wrapping Up
You learned what array_diff_uassoc does and how to pass a custom key comparison function. Here is a quick recap:
- The function compares arrays by values and keys.
- You pass a function to decide how keys compare.
- The result contains entries from the first array that do not exist in the second array.
- You can handle both string and numeric keys with your own rule.
FAQs
What is PHP array_diff_uassoc used for?
array_diff_uassoc function compares arrays with values and keys.
It uses a user-defined function to check keys.
Syntax:
array_diff_uassoc(array $array1, array $array2, callable $key_compare_func)
Example:
function compareKeys($a, $b) {
if ($a === $b) return 0;
return ($a > $b) ? 1 : -1;
}
$a = ["id" => 1, "name" => "Alex"];
$b = ["id" => 1, "name" => "John"];
print_r(array_diff_uassoc($a, $b, "compareKeys"));
What is the difference between array_diff_assoc and array_diff_uassoc?
array_diff_assoccompares keys and values directly.array_diff_uassocuses a custom function for key comparison.
$a = ["x" => 10, "y" => 20];
$b = ["x" => 10, "y" => 25];
// Direct comparison
print_r(array_diff_assoc($a, $b));
// Custom comparison
function cmp($a, $b){ return strcmp($a, $b); }
print_r(array_diff_uassoc($a, $b, "cmp"));
How does array_diff_uassoc work with multiple arrays?
array_diff_uassoc can compare one array against multiple arrays.
It removes elements that exist in any other array with matching keys.
Example:
function cmpKeys($a, $b) {
return strcmp($a, $b);
}
$a = ["id" => 101, "flag" => true];
$b = ["id" => 101];
$c = ["flag" => true];
$result = array_diff_uassoc($a, $b, $c, "cmpKeys");
print_r($result);
When should you use array_diff_uassoc in PHP?
array_diff_uassoc when:
- You need to compare arrays with keys and values.
- You require custom logic to check keys.
- Built-in functions like
array_diff_assocare not enough.
Similar Reads
The PHP null coalescing operator, written as ??, is a helpful feature added in PHP 7. It makes it easier…
PHP has great tools to handle external files such as opening, writing, deleting and more else, in this tutorial we…
Ever notice how some websites just seem to "know" you? That’s thanks to cookies! When you’re working with PHP, $_COOKIE becomes a…
PHP developers may be need to determine the number of elements in an array. The count() function in PHP makes…
The PHP do-while loop is a type of loop that executes a block of code at least once, and then…
Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…
A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…
User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…
PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…
PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…