Current Location: Home> Latest Articles> What is the return result when an empty array is passed in?

What is the return result when an empty array is passed in?

M66 2025-06-06

The array_diff_uassoc function in PHP is used to compare two or more arrays and compare key names and key values ​​based on user-defined comparison functions. This function is usually used to determine the parts of an array that are different from the key value of another array when processing data. Its syntax is as follows:

 array_diff_uassoc(array $array1, array $array2, callable $key_compare_func): array
  • $array1 is the first array.

  • $array2 is the second array used for comparison.

  • $key_compare_func is a user-defined callback function for comparing keys.

The behavior of array_diff_uassoc when passing in an empty array

When an empty array is passed as $array1 or $array2 , the return value of array_diff_uassoc will change according to the following situations:

  1. $array1 is empty, $array2 is not empty : If the first array is empty, the function will directly return an empty array. Because an empty array does not have any keys and values, it cannot be compared with any other array.

  2. $array1 is not empty, $array2 is empty : If the first array is not empty and the second array is empty, the result returned will be a copy of the first array. Because during the comparison process, the empty array does not provide any key-value pairs for comparison, all elements in the first array will be considered different from the empty array.

  3. Both arrays are empty : If both arrays are empty, the return value is also an empty array. There is no difference between the two empty arrays of keys and values, so there is no difference that can be returned.

Sample code

Here is a practical example showing the behavior of array_diff_uassoc when an empty array is passed in:

 <?php
// Define a callback function that compares key names
function compare_keys($a, $b) {
    return $a - $b;
}

// Example1: The first array is empty,The second array is non-empty
$array1 = [];
$array2 = [1 => 'apple', 2 => 'banana'];
$result = array_diff_uassoc($array1, $array2, 'compare_keys');
var_dump($result); // Output: array()

// Example2: The first array is not empty,The second array is empty
$array1 = [1 => 'apple', 2 => 'banana'];
$array2 = [];
$result = array_diff_uassoc($array1, $array2, 'compare_keys');
var_dump($result); // Output: array(1 => 'apple', 2 => 'banana')

// Example3: Both arrays are empty
$array1 = [];
$array2 = [];
$result = array_diff_uassoc($array1, $array2, 'compare_keys');
var_dump($result); // Output: array()
?>

summary

In summary, when you pass in an empty array, the behavior of array_diff_uassoc is very intuitive: if the first array is empty, the function returns an empty array; if the second array is empty, it returns a copy of the first array; if both arrays are empty, the return value is also an empty array.

In practical applications, we often need to pay attention to the input data of the array to avoid unexpected empty array situations, so that the result returned by the function call does not meet expectations.