PHP array_diff_uassoc() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_diff_uassoc() works like array_diff_assoc(), but you provide your own callback function to compare the keys. It returns the entries of the first array that are not in the others.

You use array_diff_uassoc() when keys should be treated as equal under your own rule, for example ignoring upper and lower case or ignoring a prefix. The values are still compared as strings, and only the key comparison uses your callback.

Syntax

Call array_diff_uassoc() with the values you need. Start with this line.

PHP
$result = array_diff_uassoc($a, $b, 'strcasecmp');
CallWhat it does
array_diff_uassoc($a, $b, 'strcasecmp')Compares keys without caring about upper and lower case.
array_diff_uassoc($a, $b, 'strcmp')Compares keys exactly, like array_diff_assoc() does.
array_diff_uassoc($a, $b, fn($x, $y) => $x <=> $y)Compares keys with your own function.

The callback is always the last argument, after all the arrays. It receives two keys and must return an integer: 0 when the keys count as equal, a negative number when the first is smaller and a positive number when it is larger. Built in functions such as strcmp and strcasecmp work well.

Library declarations
PHP
array_diff_uassoc(array $array, array ...$arrays, callable $key_compare_func): array

Parameters

ParameterDescription
arrayRequired. The array to compare from.
arraysOptional. One or more arrays to compare against.
key_compare_funcRequired. The callback that compares two keys. It must return 0 when the keys are equal, less than 0 when the first is smaller, and more than 0 when the first is larger.

Return Value

Return ValueDescription
arrayThe entries of the first array whose key and value pair is not found in the other arrays.

PHP Examples

Example 1 — Case Insensitive Key Comparison

Pass strcasecmp as the callback so that 'a' and 'A' count as the same key.

PHP
<?php
$a = ['a' => 'green', 'b' => 'brown', 'c' => 'blue', 'red'];
$b = ['A' => 'green', 'yellow', 'red'];

print_r(array_diff_uassoc($a, $b, 'strcasecmp'));

How It Works

  1. strcasecmp treats the keys 'a' and 'A' as equal.
  2. 'a' => 'green' therefore matches 'A' => 'green' and is removed.
  3. The keys 'b' and 'c' have no match in $b, so they are kept.
  4. 'red' at key 0 is kept because key 0 in $b holds 'yellow'.

Example 2 — With and Without the Callback

array_diff_assoc() compares keys exactly. array_diff_uassoc() uses your rule instead.

PHP
<?php
$x = ['a' => 'green'];
$y = ['A' => 'green'];

echo count(array_diff_assoc($x, $y)), PHP_EOL;
echo count(array_diff_uassoc($x, $y, 'strcasecmp')), PHP_EOL;

How It Works

  1. The two arrays differ only in the case of the key.
  2. array_diff_assoc() sees 'a' and 'A' as different keys, so one entry is left.
  3. array_diff_uassoc() with strcasecmp sees them as equal keys with equal values.
  4. The entry is removed, so the result has 0 items.

Example 3 — A Custom Callback

Write your own comparison, here one that ignores a 'user_' prefix on the keys.

PHP
<?php
$a = ['user_name' => 'Asha', 'user_city' => 'Pune'];
$b = ['name' => 'Asha', 'city' => 'Delhi'];

$compare = fn($k1, $k2) => strcmp(str_replace('user_', '', $k1), str_replace('user_', '', $k2));

print_r(array_diff_uassoc($a, $b, $compare));

How It Works

  1. The callback removes the 'user_' prefix from both keys before it compares them.
  2. 'user_name' and 'name' therefore count as the same key.
  3. Their values, 'Asha' and 'Asha', are equal too, so that entry is removed.
  4. 'user_city' matches 'city' by key, but 'Pune' and 'Delhi' differ, so it stays.

Frequently Asked Questions

It returns the entries of the first array that are not found in the other arrays, comparing values as strings and comparing keys with a callback that you provide.
It is the last argument, after all the arrays. This is different from many other PHP functions, so watch the order.
An integer. Return 0 when the two keys are equal, a negative number when the first is smaller and a positive number when the first is larger. Returning true or false is not enough.
array_diff_ukey() compares only the keys and ignores the values. array_diff_uassoc() needs the values to match as well, and uses the callback only for the keys.
Use array_udiff_assoc() to compare values with a callback while the keys are compared normally. Use array_udiff_uassoc() to use a callback for both.

Next: array_diff_ukey()

array_diff_ukey() compares the keys of the first array with the keys of other arrays by us.

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