PHP array_udiff_uassoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

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.

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

Parameters

ParameterDescription
arrayRequired. The first array.
arraysRequired. One or more arrays to compare against.
value_compare_funcRequired. Callback to compare two values. Must return an integer.
key_compare_funcRequired. Callback to compare two keys. Must return an integer.

Return Value

Return ValueDescription
arrayThe entries of the first array that have no match (by key and value) in the other arrays. Keys are preserved.

PHP Examples

Example 1 — Ignore Case in Keys and Values

Two entries match when their keys and their values are equal, ignoring case.

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

  1. The first callback compares values and the second compares keys. Both ignore case.
  2. Key "A" matches "a" and value "red" matches "RED", so that entry is removed.
  3. Key "B" matches "b", but "green" is not "blue", so it stays.
  4. The result is {"B":"green"}.

Example 2 — Change the Key Callback

The key callback alone can decide whether an entry matches.

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

  1. The value "red" is the same in both arrays, but the keys are "A" and "a".
  2. With strcmp() for keys, "A" and "a" are different, so the entry stays.
  3. With strcasecmp() for keys, they count as equal, so the entry is removed.
  4. An empty array is printed by json_encode() as [].

Example 3 — Find Items Still in Stock

Remove sold items from an inventory, ignoring case in codes and names.

PHP
<?php
$inventory = ["sku1" => "Pen", "sku2" => "Book", "sku3" => "Bag"];
$sold = ["SKU1" => "pen", "SKU3" => "BAG"];

$left = array_udiff_uassoc(
    $inventory,
    $sold,
    fn($x, $y) => strcasecmp($x, $y),
    fn($x, $y) => strcasecmp($x, $y)
);

foreach ($left as $sku => $item) {
    echo "Still in stock: ", $sku, " = ", $item, PHP_EOL;
}

How It Works

  1. The inventory lists three products by code.
  2. The sold list uses uppercase codes and different case in the names.
  3. Both callbacks ignore case, so sku1 and sku3 are matched and removed.
  4. Only sku2 is left, and the loop prints it.

Frequently Asked Questions

It finds entries of the first array that are missing from the other arrays, comparing values with one callback and keys with another.
The value callback comes first and the key callback comes last.
Use it when keys also need a custom comparison, such as ignoring case in keys. array_udiff_assoc() compares keys in a fixed way.
Yes. The returned entries keep their keys from the first array.
Yes. You can pass any number of arrays before the two callbacks.

Next: array_uintersect()

array_uintersect() returns the values of the first array that also exist in all the other .

array_uintersect() 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