PHP array_diff_uassoc: How to Compare Arrays with Keys

php array_diff_uassoc

PHP array_diff_uassoc compares arrays by values and keys. You can also pass a custom function to compare keys in your own way.

Understand the array_diff_uassoc Function in PHP

The function compares one array with another array. It checks both the values and the keys.

The syntax looks like this:

array_diff_uassoc($array1, $array2, callable $key_compare_func)
  • $array1: the main array.
  • $array2: the array to compare against.
  • $key_compare_func: a function that compares the keys.

The function returns an array. This array contains the entries from the first array that are not present in the second array.

Here is an example:

function compareKeys($a, $b) {
    if ($a === $b) return 0;
    return ($a > $b) ? 1 : -1;
}

$a1 = ["x" => 10, "y" => 20, "z" => 30];
$a2 = ["x" => 10, "y" => 25, "w" => 30];

$result = array_diff_uassoc($a1, $a2, "compareKeys");
print_r($result);

This code compares arrays by both values and keys. The result includes keys that do not match in the second array.

Here is the output:

Array
(
[y] => 20
[z] => 30
)

The function checks values and keys at the same time. Your custom function decides how the keys compare. The output shows the difference between arrays.

You use this function when you need a strict comparison between arrays. This includes cases where both keys and values must match exactly.

The custom function decides how the keys compare. You can define rules for equal, greater, or smaller. This gives you control over how the different result forms.

Examples of the array_diff_uassoc in PHP

Match Arrays with Same Keys:

function cmp($a, $b) {
    return strcmp($a, $b);
}

$arr1 = ["id" => 1, "age" => 20, "name" => "Sam"];
$arr2 = ["id" => 1, "age" => 22, "name" => "Sam"];

$out = array_diff_uassoc($arr1, $arr2, "cmp");
print_r($out);
/*
Array
(
    [age] => 20
)
*/

This example compares arrays with string keys. The function detects that the values under the key "age" differ. The result contains "age" => 20 because that does not match the second array.

Compare with Different Keys:

function keyCheck($a, $b) {
    return $a <=> $b;
}

$arr1 = ["a" => 100, "b" => 200, "c" => 300];
$arr2 = ["a" => 100, "b" => 200, "d" => 400];

$out = array_diff_uassoc($arr1, $arr2, "keyCheck");
print_r($out);

This example compares arrays with numeric values but different keys. The function shows "c" => 300 because the second array has no key "c":

Array
(
[c] => 300
)

Custom Rule for Keys:

function myKeyRule($a, $b) {
    if (strtolower($a) === strtolower($b)) return 0;
    return ($a > $b) ? 1 : -1;
}

$arr1 = ["X" => 1, "Y" => 2, "Z" => 3];
$arr2 = ["x" => 1, "y" => 2, "W" => 3];

$out = array_diff_uassoc($arr1, $arr2, "myKeyRule");
print_r($out);

This example compares keys in a case-insensitive way. The function ignores case when it checks the keys. The result only includes "Z" => 3 because the second array has no "Z" or "z".

The output:

Array
(
[Z] => 3
)

Numbers as Keys:

function compareNumKeys($a, $b) {
    return $a - $b;
}

$arr1 = [1 => "one", 2 => "two", 3 => "three"];
$arr2 = [1 => "one", 4 => "two", 5 => "three"];

$out = array_diff_uassoc($arr1, $arr2, "compareNumKeys");
print_r($out);

This example compares arrays with numbers as keys. The function detects that key 2 and 3 exist only in the first array. The result includes those unmatched pairs.

Here is the result:

Array
(
[2] => two
[3] => three
)

Wrapping Up

You learned what array_diff_uassoc does and how to pass a custom key comparison function. Here is a quick recap:

  • The function compares arrays by values and keys.
  • You pass a function to decide how keys compare.
  • The result contains entries from the first array that do not exist in the second array.
  • You can handle both string and numeric keys with your own rule.

FAQs

What is PHP array_diff_uassoc used for?

The array_diff_uassoc function compares arrays with values and keys. It uses a user-defined function to check keys. Syntax:
array_diff_uassoc(array $array1, array $array2, callable $key_compare_func)
Example:
function compareKeys($a, $b) {
    if ($a === $b) return 0;
    return ($a > $b) ? 1 : -1;
}
$a = ["id" => 1, "name" => "Alex"];
$b = ["id" => 1, "name" => "John"];
print_r(array_diff_uassoc($a, $b, "compareKeys"));

What is the difference between array_diff_assoc and array_diff_uassoc?

Main difference:
  • array_diff_assoc compares keys and values directly.
  • array_diff_uassoc uses a custom function for key comparison.
Example:
$a = ["x" => 10, "y" => 20];  
$b = ["x" => 10, "y" => 25];  

// Direct comparison
print_r(array_diff_assoc($a, $b));  

// Custom comparison
function cmp($a, $b){ return strcmp($a, $b); }  
print_r(array_diff_uassoc($a, $b, "cmp"));

How does array_diff_uassoc work with multiple arrays?

array_diff_uassoc can compare one array against multiple arrays. It removes elements that exist in any other array with matching keys. Example:
function cmpKeys($a, $b) {
    return strcmp($a, $b);
}
$a = ["id" => 101, "flag" => true];
$b = ["id" => 101];
$c = ["flag" => true];
$result = array_diff_uassoc($a, $b, $c, "cmpKeys");
print_r($result);

When should you use array_diff_uassoc in PHP?

Use array_diff_uassoc when:
  1. You need to compare arrays with keys and values.
  2. You require custom logic to check keys.
  3. Built-in functions like array_diff_assoc are not enough.

Similar Reads

PHP filter_var: Understand How to Sanitize Input

PHP didn’t have a way to check or clean user input. Developers used scattered code—some wrote custom checks, others used…

PHP Functions: The Complete Guide for Beginners

So, what is a function? Quite simply, a function in PHP is a set of instructions you write once, and…

PHP File Inclusion: require, include, require_once, include_once

PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

PHP mb_strtolower: How to Handle Multibyte Strings Properly

The mb_strtolower() function in PHP turns every letter in a string into lowercase. It works with multibyte encodings like UTF-8.…

PHP continue Statement: How to Skip Steps in Iteration

The PHP continue statement skips the rest of the loop in the current cycle. It jumps to the next cycle…

PHP Namespace: How to Group Code (With Example)

PHP namespace solves the problem of name conflicts. Different developers may create functions-classes, or constants with the same name. PHP…

PHP range Function: Create Arrays with Sequence Values

The PHP range function helps you make sequences. Without it, you would write loops every time you need numbers or…

PHP array_merge_recursive: Merge Arrays Deeply

PHP array_merge_recursive joins two or more arrays into one nested array and keeps all values with their keys. Syntax of…

PHP echo vs print: Key Differences & Usage Guide

Echo and print are foundational in displaying output in PHP, and though they might be similar in some ways, they…

PHP fread Function: How to Read Files in PHP

In some cases, you need to handle a lot of data or simply try to open a file, or read…

Previous Article

JavaScript this Keyword: How It Works in Depth with Examples

Next Article

HTML Breadcrumb Navigation Guide for Beginners

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.