Home PHP Array array_udiff_uassoc() PHP array_udiff_uassoc() Function Definition In PHP, array_udiff_uassoc() returns the items of the first array that are not in the other arrays, using one callback to compare values and another to compare keys.
It gives you full control over both sides. For example, you can ignore case in the values and in the keys at the same time.
Syntax Call array_udiff_uassoc() with the values you need. Start with this line.
$diff = array_udiff_uassoc($a, $b, 'strcasecmp', 'strcasecmp'); Call What it does array_udiff_uassoc($a, $b, 'strcasecmp', 'strcasecmp')Ignores case in both keys and values. array_udiff_uassoc($a, $b, 'strcasecmp', 'strcmp')Ignores case in values only. array_udiff_uassoc($a, $b, $valueCmp, $keyCmp)Uses your own two callbacks.
The value callback comes first, then the key callback. Both are placed after all the arrays. This is the most flexible of the diff functions.
Library declarations array_udiff_uassoc(array $array, array ...$arrays, callable $value_compare_func, callable $key_compare_func): array Parameters Parameter Description array Required. The first array. arrays Required. One or more arrays to compare against. value_compare_func Required. Callback to compare two values. Must return an integer. key_compare_func Required. Callback to compare two keys. Must return an integer.
Return Value Return Value Description array The entries of the first array that have no match (by key and value) in the other arrays. Keys are preserved.
PHP Examples 1. Ignore Case in Keys and Values 2. Change the Key Callback 3. Find Items Still in Stock Example 1 — Ignore Case in Keys and Values Two entries match when their keys and their values are equal, ignoring case.
<?php
$a = ["A" => "red", "B" => "green"];
$b = ["a" => "RED", "b" => "blue"];
$diff = array_udiff_uassoc(
$a,
$b,
fn($x, $y) => strcasecmp($x, $y),
fn($x, $y) => strcasecmp($x, $y)
);
echo json_encode($diff), PHP_EOL; How It Works The first callback compares values and the second compares keys. Both ignore case.Key "A" matches "a" and value "red" matches "RED", so that entry is removed. Key "B" matches "b", but "green" is not "blue", so it stays. The result is {"B":"green"}. Example 2 — Change the Key Callback The key callback alone can decide whether an entry matches.
<?php
$a = ["A" => "red"];
$b = ["a" => "red"];
$value = fn($x, $y) => strcasecmp($x, $y);
echo json_encode(array_udiff_uassoc($a, $b, $value, fn($x, $y) => strcmp($x, $y))), PHP_EOL;
echo json_encode(array_udiff_uassoc($a, $b, $value, fn($x, $y) => strcasecmp($x, $y))), PHP_EOL; How It Works The value "red" is the same in both arrays, but the keys are "A" and "a". With strcmp() for keys, "A" and "a" are different, so the entry stays. With strcasecmp() for keys, they count as equal, so the entry is removed. An empty array is printed by json_encode() as []. Frequently Asked Questions What does array_udiff_uassoc() do? It finds entries of the first array that are missing from the other arrays, comparing values with one callback and keys with another.
Which callback comes first? The value callback comes first and the key callback comes last.
When should I use it instead of array_udiff_assoc()? Use it when keys also need a custom comparison, such as ignoring case in keys. array_udiff_assoc() compares keys in a fixed way.
Are keys preserved in the result? Yes. The returned entries keep their keys from the first array.
Can I pass more than two arrays? Yes. You can pass any number of arrays before the two callbacks.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion