Current Location: Home> Latest Articles> The difference between array_diff_ukey() and array_diff_key()

The difference between array_diff_ukey() and array_diff_key()

M66 2025-05-15

In PHP, array_diff_ukey() and array_diff_key() are functions used to compare key names of arrays and return key names differences. Although the functions of these two functions are very similar, there are some significant differences in how they are used. This article will explain the differences between these two functions in detail and show their application in actual development.

1. array_diff_key() function

The array_diff_key() function is used to compare the key names of two or more arrays, and returns the key name and key value pairs that exist in the first array but not in other arrays. That is, it returns all elements whose key names do not exist in other arrays.

grammar:

 array_diff_key(array $array1, array $array2, array ...$arrays): array

Sample code:

 $array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];

$array2 = [
    'b' => 5,
    'd' => 6
];

$result = array_diff_key($array1, $array2);
print_r($result);

Output:

 Array
(
    [a] => 1
    [c] => 3
)

In this example, the array_diff_key() function returns a key-value pair that exists in the array $array1 but does not exist in $array2 .

2. array_diff_ukey() function

The array_diff_ukey() function is similar to array_diff_key() , but it allows the user to pass a callback function to compare key names. This provides users with more flexibility, especially when custom key name comparison rules are required.

grammar:

 array_diff_ukey(array $array1, array $array2, callable $key_compare_func): array

Sample code:

 $array1 = [
    'a' => 1,
    'b' => 2,
    'c' => 3
];

$array2 = [
    'A' => 5,
    'B' => 6
];

// Custom key name comparison function
$key_compare_func = function ($key1, $key2) {
    return strtolower($key1) <=> strtolower($key2);
};

$result = array_diff_ukey($array1, $array2, $key_compare_func);
print_r($result);

Output:

 Array
(
    [c] => 3
)

In this example, array_diff_ukey() compares key names based on a custom callback function. The callback function uses strtolower to compare the key name to lowercase, and the result only returns the key name c element because it does not have the same lowercase key name in $array2 .

3. Main differences

  1. Key name comparison method:

    • array_diff_key() directly uses PHP's default comparison method to compare the key names of the array.

    • array_diff_ukey() allows users to customize the comparison rules for key names, and can implement custom key name comparisons by passing a callback function.

  2. flexibility:

    • array_diff_key() does not allow custom key name comparison rules, its comparison is based on PHP's default equality comparison.

    • array_diff_ukey() is more flexible, and you can define a custom key name comparison method through a callback function.

  3. Application scenarios:

    • array_diff_key() is suitable for cases where key names are directly equal.

    • array_diff_ukey() is suitable for situations where key names require custom comparison rules, such as ignoring upper and lower case, comparing according to some algorithm, etc.

4. Practical application

In actual development, array_diff_key() is more commonly used because it is simple to use and is suitable for most common array comparison scenarios. array_diff_ukey() is suitable for special needs, such as ignoring the case of key names or performing other custom comparison operations.

Example: Comparison of key names that ignore upper and lower case

Suppose you have two arrays whose key names are actually the same, but one array has the uppercase and the other is lowercase. In this case, you can use array_diff_ukey() to compare their key names, ignoring case.

 $array1 = [
    'apple' => 1,
    'banana' => 2
];

$array2 = [
    'APPLE' => 3,
    'BANANA' => 4
];

// Ignore case comparison
$key_compare_func = function ($key1, $key2) {
    return strtolower($key1) <=> strtolower($key2);
};

$result = array_diff_ukey($array1, $array2, $key_compare_func);
print_r($result);

Output:

 Array
(
)

In this example, since the key names apple and APPLE are considered the same, the result returns an empty array, indicating that there is no difference in the key names of the two arrays.

5. Summary

  • array_diff_key() is a simple and direct way to compare array key names, suitable for regular key name differences.

  • array_diff_ukey() provides more flexibility, allowing custom key names comparison rules, suitable for scenarios where special comparison logic is required.

  • Which function to choose depends on your needs. If you just need to compare the direct equality of key names, array_diff_key() is enough. If you need more complex comparison rules, array_diff_ukey() will be a better choice.