Current Location: Home> Latest Articles> array_diff_ukey() vs array_diff(): When to use which one?

array_diff_ukey() vs array_diff(): When to use which one?

M66 2025-05-15

In PHP, array_diff() and array_diff_ukey() are both used to compare arrays, but they have obvious differences in their working principles and usage scenarios. This article will discuss the differences between these two functions in detail and analyze their usage scenarios.

1. array_diff() function

The array_diff() function is used to compare the values ​​of two or more arrays, returning elements that exist in the first array but not in other arrays. This function is compared based on the value, regardless of the key name.

grammar:
 array_diff(array $array1, array $array2, array ...$arrays): array
Example:
 $array1 = ["apple" => "green", "banana" => "yellow", "cherry" => "red"];
$array2 = ["banana" => "yellow", "cherry" => "blue", "grape" => "purple"];

$result = array_diff($array1, $array2);
print_r($result);
Output:
 Array
(
    [apple] => green
)

In this example, array_diff() returns an element that exists in the array $array1 but not in $array2 . Note that array_diff() only compares values ​​and does not compare keys.

2. array_diff_ukey() function

Unlike array_diff() , the array_diff_ukey() function is compared based on the key names of the array. It compares whether the key in the first array exists in other arrays, and only excludes the element from the result if the key names do not match.

grammar:
 array_diff_ukey(array $array1, array $array2, callable $key_compare_func): array
Example:
 $array1 = ["apple" => "green", "banana" => "yellow", "cherry" => "red"];
$array2 = ["banana" => "yellow", "cherry" => "blue", "grape" => "purple"];

$result = array_diff_ukey($array1, $array2, "key_compare");
print_r($result);

function key_compare($key1, $key2) {
    return $key1 === $key2 ? 0 : -1;
}
Output:
 Array
(
    [apple] => green
)

In this example, array_diff_ukey() is compared by the key name, thus returning the "apple" key in $array1 because it does not exist in $array2 .

3. Summary of the difference

  • array_diff() : Compare the values ​​of the array and ignore the key names. Applicable to if you only care about whether the value exists in other arrays.

  • array_diff_ukey() : Compare the key names of the array and ignore the value. It works if you care about the existence of the key name, not whether the values ​​are the same.

4. When to use array_diff() or array_diff_ukey() ?

  • Use array_diff() : When you need to compare the values ​​of an array, using array_diff() is a more suitable choice. For example, you can use this function when you want to find unique values ​​in an array.

  • Use array_diff_ukey() : When you need to compare the key names of an array, it is more appropriate to use array_diff_ukey() . It can help you filter out certain keys that don't care about the content of the value. For example, if you have an associative array and only care about whether certain specific keys exist in other arrays, you can use this function.

Summarize

The main difference between array_diff() and array_diff_ukey() is that they compare different objects: the former is compared based on the value of the array, while the latter is compared based on the key name of the array. Depending on your actual needs, choosing the right function can help you process arrays more efficiently.