W3docs

PHP Array Diff Function - A Comprehensive Guide

In PHP, the array_diff_ukey function is a powerful tool that can help you compare arrays and return the difference between them based on a custom key comparison

PHP array_diff_ukey() Function - A Comprehensive Guide

In PHP, the array_diff_ukey() function is a powerful tool that can help you compare arrays and return the difference between them based on a custom key comparison function. In this article, we'll take a closer look at how this function works, and how you can use it in your PHP projects.

Function Signature & Parameters

array_diff_ukey(array $array, array ...$arrays, callable $key_compare_func): array

This is the PHP manual's notation for the function. At runtime the last argument you pass is always the comparison callback; every argument before it is treated as an array.

Parameters:

  • $array: The base array to compare against.
  • $arrays: One or more arrays to compare with the base array.
  • $key_compare_func: A callable that compares two keys and returns an integer less than, equal to, or greater than zero (the same contract as a usort comparator).

Returns: An array containing the entries from $array whose keys are not present in any of the other arrays. Values are never inspected — only keys.

When would I use it?

Most of the time array_diff_key() is enough: it removes entries whose keys appear in other arrays using a plain ===-style key comparison. Reach for array_diff_ukey() only when "is this key the same?" needs custom logic — for example treating keys case-insensitively, comparing only a key's prefix, or normalizing numeric strings before matching. If you need to compare values with a callback instead, see array_udiff(); to compare both keys and values, see array_diff_uassoc().

How Does the PHP array_diff_ukey() Function Work?

The array_diff_ukey() function takes two or more arrays as arguments, and compares their entries based on the key comparison function you provide. The key comparison function should return an integer less than, equal to, or greater than zero, depending on the result of the comparison.

If the comparison result is less than zero, the first argument is considered less than the second. If the result is greater than zero, the first argument is considered greater than the second. If the result is equal to zero, the two arguments are considered equal.

Using the PHP array_diff_ukey() Function

Here's an example of how you can use the array_diff_ukey() function in PHP:

PHP example of array_diff_ukey function usage

<?php

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

$array1 = ["a" => "green", "b" => "brown", "c" => "blue", "red"];
$array2 = ["a" => "green", "yellow", "red"];
$result = array_diff_ukey($array1, $array2, 'key_compare_func');
print_r($result);

?>

In this example, the array_diff_ukey() function is used to compare the entries in $array1 and $array2 based on a custom key comparison function named key_compare_func. The resulting array, stored in $result, contains the entries from $array1 that are not present in $array2.

Note: In the callback, $a and $b represent the keys from the arrays, not the values.

Output:

Array
(
    [b] => brown
    [c] => blue
)

Walk through it: $array1 has keys a, b, c, and 0 (the un-keyed "red" gets the integer key 0). $array2 has keys a, 0, and 1. The keys a and 0 exist in both arrays, so array_diff_ukey() drops "green" and "red" from the result, leaving only the entries whose keys (b, c) are unique to $array1.

Custom Key Comparison Function

The custom key comparison function is a critical part of the array_diff_ukey() function, as it determines how the entries in the two arrays are compared. Here's an example of a custom key comparison function:

PHP Example of custom array key compare function

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

In this example, the key_compare_func() function returns 0 if the two arguments are equal, 1 if the first argument is greater than the second, and -1 if the first argument is less than the second.

Note on Type Juggling: When comparing keys of mixed types (e.g., strings and integers), PHP's loose comparison in your callback may lead to unexpected results. Ensure your callback explicitly handles type casting or uses strict comparison (===) if precise type matching is required.

A practical case: comparing keys case-insensitively

The example above behaves just like the simpler array_diff_key(). The custom callback only earns its keep when the comparison itself is non-trivial — for instance, matching keys regardless of letter case:

php— editable, runs on the server

Even though "Host" and "PORT" differ in case from "host" and "port", strcasecmp treats them as equal keys, so they are removed. Only Debug, which has no counterpart in $defaults, survives:

Array
(
    [Debug] => 1
)

A plain array_diff_key() here would have kept all three entries, because "Host" !== "host". That difference is precisely when array_diff_ukey() is the right tool.

Conclusion

The array_diff_ukey() function compares arrays by their keys using a callback you supply, returning the entries from the first array whose keys are absent from the others. Use it when key equality needs custom logic; reach for the plainer array_diff_key() when a strict key comparison is enough.

Related functions worth knowing:

Practice

Practice
What is the correct usage of array_diff_ukey() function in PHP?
What is the correct usage of array_diff_ukey() function in PHP?
Was this page helpful?