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 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 array_diff_ukey Function: How it Works with Examples

The PHP array_diff_ukey function compares keys in two or more arrays. It returns the keys from the first array that…

Class and Object in PHP: How They Work with a Real Example

Classes and objects are important parts of OOP in PHP. They help you organize your code so you can update…

PHP MySQL ORDER BY: How to Sort Data in SQL?

Sorting data is one of those things we do all the time but rarely stop to think about. Whether you…

PHP strlen Function: How It Works with Strings

Developers needed a way to check the length of a string, so the strlen function was introduced to handle this…

PHP sizeof: How to Count Elements in Arrays with Examples

The sizeof function in PHP exists to help developers know how many items an array holds. Understand the sizeof Function…

PHP fclose(): Close Access of File Manipulation

Actually, PHP has a built-in function that doesn’t get the spotlight—fclose(). It ends the file manipulation code. Let’s break down…

PHP implode Function: Join Array Values into a String

The implode function in PHP appeared to solve a common need—joining array elements into a single string. It helps format…

Inheritance in PHP: Share Code from Class to Class & Examples

PHP developers copied and pasted code across multiple files before inheritance came, which made updates difficult. They updated functions in…

PHP cURL: HTTP Requests and Responses in PHP

In PHP, there are plenty of times when you need to connect with other servers or APIs. That's where the…

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.