Current Location: Home> Latest Articles> How to correctly use array_diff_ukey() function and customize the callback function?

How to correctly use array_diff_ukey() function and customize the callback function?

M66 2025-05-15

In PHP, the array_diff_ukey() function is used to compare keys of two or more arrays and return elements that are different from those of other arrays. This function is very useful, especially if we need to compare keys of an array through custom rules. The array_diff_ukey() function allows us to specify a callback function to customize the rules for key comparison. This article will introduce how to use the array_diff_ukey() function correctly and show how to use a custom callback function to compare keys.

Introduction to array_diff_ukey() function

Function prototype

 array_diff_ukey(array $array1, array $array2, callable $key_compare_func): array
  • array1 : The first array, used for comparison.

  • array2 : The second array, other arrays can also be passed in as comparison objects.

  • key_compare_func : A callback function for customizing the comparison rules of keys. This function takes two parameters, represents the keys in two arrays, and returns an integer value:

    • If the first key is smaller than the second key, a negative value is returned.

    • If the first key is greater than the second key, a positive value is returned.

    • If the two keys are equal, return zero.

Example: Basic usage

First, let's look at a simple example to understand the basic usage of the array_diff_ukey() function.

 <?php
$array1 = [
    1 => 'apple',
    2 => 'banana',
    3 => 'cherry',
];

$array2 = [
    3 => 'cherry',
    4 => 'date',
];

$result = array_diff_ukey($array1, $array2, function($key1, $key2) {
    return $key1 - $key2;
});

print_r($result);
?>

Output result:

 Array
(
    [1] => apple
    [2] => banana
)

In this example, we compare the keys in array1 and array2 and filter according to the rules in the callback function. Since key1 and key2 are numeric keys, and we choose to use the default numerical comparison rule, we finally return the elements corresponding to the keys not in array2 .

Use custom callback functions

The power of the array_diff_ukey() function is that you can change the way keys are compared by customizing the callback function. Callback functions can be compared based on different conditions, such as alphabetical order, by numerical size, or according to other business rules.

Example: Compare keys alphabetically

Suppose we have two arrays and want to compare the keys according to alphabetical order:

 <?php
$array1 = [
    'apple' => 'fruit1',
    'banana' => 'fruit2',
    'cherry' => 'fruit3',
];

$array2 = [
    'cherry' => 'fruit3',
    'date' => 'fruit4',
];

$result = array_diff_ukey($array1, $array2, function($key1, $key2) {
    return strcmp($key1, $key2);
});

print_r($result);
?>

Output result:

 Array
(
    [apple] => fruit1
    [banana] => fruit2
)

In this example, we use the strcmp() function to compare the keys of the array ( 'apple' , 'banana' , etc.). The strcmp() function compares strings alphabetically, and the return value determines the order and equality of the two keys.

Example: Comparison of lengths of keys

We can also compare the size of the key according to the length of the key:

 <?php
$array1 = [
    'a' => 'apple',
    'banana' => 'fruit',
    'cherry' => 'berry',
];

$array2 = [
    'cherry' => 'fruit',
    'date' => 'sweet',
];

$result = array_diff_ukey($array1, $array2, function($key1, $key2) {
    return strlen($key1) - strlen($key2);
});

print_r($result);
?>

Output result:

 Array
(
    [banana] => fruit
)

In this example, we use the strlen() function to compare the length of the key. In this way, some more complex custom comparison rules can be implemented.

in conclusion

Through the array_diff_ukey() function, you can flexibly compare the keys of an array and use the callback function to define your own comparison rules. This makes array operations more flexible and powerful, and can deal with various complex scenarios.