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 $_REQUEST: Learn How to Capture User Inputs

Today, we will discuss the most prominent and useful superglobal in PHP, $_REQUEST, to get user input. The $_REQUEST array lets you retrieve…

PHP Data Types: Understanding the 10 Primitive Types

The whole world of PHP programming revolves around data, be it numbers, text, or more complicated structures. Each of these…

PHP $_SERVER: Basics and Examples

PHP $_SERVER is a superglobal. It’s a predefined variable containing information about your server, client, and request environment. As a…

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…

PHP filter_list(): List Available Filters

PHP introduces the filter_list() function to give developers a way to check all available filters in the filter extension. This…

PHP XML Expat Parser: A Complete Guide

If you've ever tried to pull data from an XML file, you know it can be a bit tricky at…

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 OOP Constructor: How It Works in a Class with Examples

The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

Mastering PHP’s Do-While Loop: Examples and Explanation

The PHP do-while loop is a type of loop that executes a block of code at least once, and then…

PHP IF Statement: A Guide to Conditional Logic

The IF statement in PHP helps to see if a condition is true or not. If it is true, it…

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.