Current Location: Home> Latest Articles> What is the difference between array_udiff_uassoc() and array_diff_uassoc()? What are their usage scenarios and functions?

What is the difference between array_udiff_uassoc() and array_diff_uassoc()? What are their usage scenarios and functions?

M66 2025-06-06

In PHP, array_udiff_uassoc() and array_diff_uassoc() are functions used to compare arrays, but they have different functions, uses and implementation methods. In this article, we will dig into the differences between these two functions and discuss their usage scenarios and functions.

1. Function introduction

  1. array_udiff_uassoc()
    The array_udiff_uassoc() function returns the difference between two or more arrays, while comparing key names and key values. This function can determine the differences between arrays through custom key-value comparison functions.

    Function prototype:

     array_udiff_uassoc(array $array1, array $array2, callable $value_compare_func, callable $key_compare_func): array
    
    • parameter :

      • $array1, $array2 : The array that needs to be compared.

      • $value_compare_func : User defined comparison function, equality of comparison values.

      • $key_compare_func : User-defined comparison function, equality of comparison keys.

    This function will not only compare based on the value, but also based on the key name, which is suitable for scenarios where precise control of the value and key comparison logic is required.

  2. array_diff_uassoc()
    The array_diff_uassoc() function is similar to array_udiff_uassoc() , but it only considers the values ​​and keys of the array, and does not allow users to customize the comparison logic. It compares the differences between the two arrays and returns elements that appear only in the first array.

    Function prototype:

     array_diff_uassoc(array $array1, array $array2, callable $key_compare_func): array
    
    • parameter :

      • $array1, $array2 : The array that needs to be compared.

      • $key_compare_func : A function used to compare whether the key names are equal.

    This function compares the differences in key names of arrays through a custom key comparison function, which is suitable for scenarios where only custom key comparison logic is required.

2. Functional differences

  1. Comparison content

    • array_udiff_uassoc() will compare the key value and key name of the array at the same time, which is suitable for situations where you need to customize how to compare array keys and values.

    • array_diff_uassoc() only compares the key name differences of the array and allows you to customize the way to compare keys.

  2. Number of parameters

    • array_udiff_uassoc() can accept multiple arrays for comparison, while array_diff_uassoc() only compares the differences between two arrays.

3. Use scenarios

1. Array_udiff_uassoc() usage scenario

Suppose you have two arrays, one with the user's ID and name, and the other with the user's ID and mailbox, and you need to compare these two arrays, taking into account the differences in keys and values, and using custom comparison rules.

 $array1 = [
    1 => 'John Doe',
    2 => 'Jane Smith',
    3 => 'George Brown'
];

$array2 = [
    1 => 'John Doe',
    2 => 'Jane S.',
    4 => 'Alice Green'
];

$value_compare_func = function ($a, $b) {
    return strcmp($a, $b);
};

$key_compare_func = function ($a, $b) {
    return $a - $b;
};

$result = array_udiff_uassoc($array1, $array2, $value_compare_func, $key_compare_func);
print_r($result);

Output:

 Array
(
    [3] => George Brown
)

In this example, array_udiff_uassoc() compares the values ​​and keys of the two arrays, and finally returns the difference term in array1 .

2. Array_diff_uassoc() usage scenario

If you only need to compare arrays based on the differences in keys, and don't care about the differences in values, array_diff_uassoc() is a more suitable choice. For example, suppose you have two arrays containing user ID and other information, and you want to find keys that exist in the first array but not in the second array.

 $array1 = [
    1 => 'John Doe',
    2 => 'Jane Smith',
    3 => 'George Brown'
];

$array2 = [
    1 => 'John Doe',
    4 => 'Alice Green'
];

$key_compare_func = function ($a, $b) {
    return $a - $b;
};

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

Output:

 Array
(
    [2] => Jane Smith
    [3] => George Brown
)

In this example, array_diff_uassoc() returns only the elements in array1 based on the difference in keys.

4. Summary

characteristic array_udiff_uassoc() array_diff_uassoc()
Comparison content Values ​​and keys Key only
Whether to allow custom comparison functions Yes (values ​​and keys can be compared with custom) Yes (only custom key comparisons are allowed)
Applicable scenarios The difference between values ​​and keys needs to be considered Just care about the differences in keys

In general, choosing to use array_udiff_uassoc() or array_diff_uassoc() depends on whether you need to compare custom values ​​and whether you need to compare values ​​with keys. Understanding the difference between these two functions will help you make the right choice based on your specific needs.