Current Location: Home> Latest Articles> The callback function is only valid for key names, and the errors of value are ignored

The callback function is only valid for key names, and the errors of value are ignored

M66 2025-06-06

The prototype of array_diff_uassoc() is as follows:

 array array_diff_uassoc ( array $array1 , array $array2 , callable $key_compare_func )
  • $array1 and $array2 : Two arrays to be compared.

  • $key_compare_func : Custom callback function to compare keys in arrays.

The function returns an array containing all key-value pairs in $array1 that have no matching keys in $array2 . Importantly, the comparison here is based on the key name and the custom comparison function .

How does a callback function work?

The definition of the callback function key_compare_func is as follows:

 function key_compare_func ($key1, $key2) {
    // The logic of comparison keys
}

In array_diff_uassoc() , this callback function only compares the key names , and has nothing to do with the value. The purpose of this function is to enable developers to define how to compare keys in arrays according to their own needs.

Specific workflow:

  1. array_diff_uassoc() first compares the key names in $array1 and $array2 .

  2. If the key name in $array1 does not exist in $array2 , or the result of comparing the key names through the callback function is unequal, then the key-value pair in $array1 will be retained.

  3. It should be noted that this comparison is entirely based on the key name and the values ​​do not participate in the comparison.

Why are the values ​​ignored?

We need to return to PHP's design philosophy to understand this behavior. The main purpose of the array_diff_uassoc() function is to calculate the differences in the array based on the key name . If it takes into account the difference in key values ​​at the same time, its function overlaps with array_diff() or array_diff_assoc() .

  1. The design purpose is clear : The original intention of array_diff_uassoc() is to provide a comparison of the differences in key names, not just the differences in values. The value comparison function has been well implemented in array_diff() and array_diff_assoc() .

  2. Limitations of callback functions : The callback function key_compare_func is just a comparison of key names, not values. PHP's design philosophy is usually to let each function do a single thing so that the behavior of each function is clearer.

  3. Efficiency considerations : If a callback function compares keys and values ​​at the same time, the implementation of the function may become more complex and inefficient. PHP allows each function to complete tasks efficiently and simply by comparing keys and values.

Sample code

Suppose we have the following array:

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

$array2 = [
    1 => 'apple',
    2 => 'orange',
    4 => 'grape',
];

If we use array_diff_uassoc() to compare them, the callback function will only compare the key names:

 function compare_keys($key1, $key2) {
    return $key1 - $key2;
}

$result = array_diff_uassoc($array1, $array2, 'compare_keys');

print_r($result);

Output result :

 Array
(
    [3] => cherry
)

In this example, array_diff_uassoc() compares the key names of the array $array1 and $array2 to find the keys that are not in $array2 . In the result 3 => cherry is preserved because the key 3 does not exist in $array2 , although their values ​​are different. As expected, the callback function is only valid for key names and the values ​​are not participated in the comparison.

Summarize

The reason why the array_diff_uassoc() callback function is only valid for key names and ignores the value is mainly because the design goal of the function is to perform differential calculations based on the key names. This behavior helps simplify the logic of the function, making it more efficient and focused on its goal: the difference in key names. If you need to make a differential comparison of key values, you can use other functions like array_diff_assoc() , or process the value and key names in the callback function.