PHP array_udiff_assoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

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.

PHP
$diff = array_udiff_assoc($a, $b, 'strcasecmp');
CallWhat 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
PHP
array_udiff_assoc(array $array, array ...$arrays, callable $value_compare_func): array

Parameters

ParameterDescription
arrayRequired. The first array.
arraysRequired. One or more arrays to compare against.
value_compare_funcRequired. Callback that compares two values and returns an integer (negative, zero or positive).

Return Value

Return ValueDescription
arrayThe entries of the first array whose key and value are not found in the other arrays. Keys are preserved.

PHP Examples

Example 1 — Compare Keys and Values

Keep only the entries that differ, ignoring case in the values.

PHP
<?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

  1. Both arrays use the keys a, b and c.
  2. Key a: "Red" and "red" are equal when case is ignored, so it is removed.
  3. Key b: "Green" and "yellow" are different, so the entry stays.
  4. 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
<?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;

How It Works

  1. The value "1" appears in both arrays, but under different keys (x and z).
  2. array_udiff_assoc() needs the key and the value to match, so nothing is removed.
  3. array_udiff() looks at values only, so "1" is removed from the result.
  4. That is the key difference between the two functions.

Example 3 — Find Changed Settings

Compare new settings with old ones and show only the changes.

PHP
<?php
$old = ["theme" => "Dark", "lang" => "EN", "size" => "12"];
$new = ["theme" => "dark", "lang" => "FR", "size" => "12"];

$changes = array_udiff_assoc($new, $old, fn($x, $y) => strcasecmp($x, $y));

foreach ($changes as $key => $value) {
    echo $key, " changed to ", $value, PHP_EOL;
}

How It Works

  1. The new settings are the first array, so we see what is new.
  2. theme differs only by case, so the callback treats it as unchanged.
  3. lang changed from EN to FR, and size stayed the same.
  4. Only lang remains, and the loop prints it.

Frequently Asked Questions

It computes the difference between arrays using both keys and values. Values are compared with your callback and keys with the built-in comparison.
array_udiff() ignores keys. array_udiff_assoc() requires the key to match as well before it counts an entry as equal.
Not with this function. Use array_udiff_uassoc() and pass a second callback for the keys.
Yes. Entries in the result keep the keys they had in the first array.
An integer: negative, zero or positive. Zero means the two values are equal.

Next: array_udiff_uassoc()

array_udiff_uassoc() returns the items of the first array that are not in the other arrays.

array_udiff_uassoc() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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