Home PHP Array array_udiff_assoc() PHP array_udiff_assoc() Function Definition In PHP, array_udiff_assoc() returns the items of the first array that are not in the other arrays, comparing both keys and values, with a custom function for the values.
An item is only treated as a match when the key is the same and your callback says the values are equal. Keys are compared with PHP's built-in comparison.
Syntax Call array_udiff_assoc() with the values you need. Start with this line.
$diff = array_udiff_assoc($a, $b, 'strcasecmp'); Call What it does array_udiff_assoc($a, $b, 'strcasecmp')Compares keys exactly and values ignoring case. array_udiff_assoc($new, $old, $compare)Finds settings whose value changed. array_udiff_assoc($a, $b, $c, $compare)Compares against several arrays.
Think of it as array_diff_assoc() with a custom value comparison. If you need a custom key comparison too, use array_udiff_uassoc().
Library declarations array_udiff_assoc(array $array, array ...$arrays, callable $value_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 that compares two values and returns an integer (negative, zero or positive).
Return Value Return Value Description array The entries of the first array whose key and value are not found in the other arrays. Keys are preserved.
PHP Examples 1. Compare Keys and Values 2. Same Value, Different Key 3. Find Changed Settings Example 1 — Compare Keys and Values Keep only the entries that differ, ignoring case in the values.
<?php
$a = ["a" => "Red", "b" => "Green", "c" => "Blue"];
$b = ["a" => "red", "b" => "yellow", "c" => "blue"];
$diff = array_udiff_assoc($a, $b, fn($x, $y) => strcasecmp($x, $y));
echo json_encode($diff), PHP_EOL; How It Works Both arrays use the keys a, b and c. Key a: "Red" and "red" are equal when case is ignored, so it is removed. Key b: "Green" and "yellow" are different, so the entry stays. Key c matches like key a. Only {"b":"Green"} remains.Example 2 — Same Value, Different Key Compare array_udiff() with array_udiff_assoc() on the same data.
<?php
$a = ["x" => "1", "y" => "2"];
$b = ["z" => "1"];
$compare = fn($p, $q) => strcmp($p, $q);
echo json_encode(array_udiff_assoc($a, $b, $compare)), PHP_EOL;
echo json_encode(array_udiff($a, $b, $compare)), PHP_EOL; {"x":"1","y":"2"}
{"y":"2"} How It Works The value "1" appears in both arrays, but under different keys (x and z). array_udiff_assoc() needs the key and the value to match, so nothing is removed. array_udiff() looks at values only, so "1" is removed from the result. That is the key difference between the two functions. Frequently Asked Questions What does array_udiff_assoc() do? It computes the difference between arrays using both keys and values. Values are compared with your callback and keys with the built-in comparison.
How is it different from array_udiff()? array_udiff() ignores keys. array_udiff_assoc() requires the key to match as well before it counts an entry as equal.
Can I ignore case in the keys too? Not with this function. Use array_udiff_uassoc() and pass a second callback for the keys.
Are keys preserved in the result? Yes. Entries in the result keep the keys they had in the first array.
What should the callback return? An integer: negative, zero or positive. Zero means the two values are equal.
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