In PHP, the array_diff_uassoc() function is used to compare the differences between two arrays and can customize the rules for comparing keys. The basic syntax of this function 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, and is compared with $array1 .
$key_compare_func is a callback function that compares two keys.
The array_diff_uassoc() function returns an array containing elements that exist in $array1 but do not exist in $array2 . Its comparison is based on the key name (using a custom callback function). But you may encounter some questions: When the return value of key_compare_func is 0 , -1 or 1 , what impact will these different return values have on the result of the function?
When the key_compare_func callback function returns 0 , it means that the two keys are equal. In the array_diff_uassoc() function, this means that the current key value pairs of $array1 and $array2 have no difference in key names. Therefore, these key-value pairs are not considered part of the difference and do not appear in the returned result array.
Example:
$array1 = [
'apple' => 'red',
'banana' => 'yellow'
];
$array2 = [
'apple' => 'green',
'banana' => 'yellow'
];
function compare_keys($key1, $key2) {
return 0; // It means that the two keys are the same
}
$result = array_diff_uassoc($array1, $array2, 'compare_keys');
print_r($result);
Output:
Array
(
[apple] => red
)
Explanation: Since compare_keys() returns 0 , it means that the key names apple and banana are equal, array_diff_uassoc() believes that they have no difference, so the return array contains only the value 'red' corresponding to the key apple .
When key_compare_func returns -1 , it means that the key in $array1 is smaller than the key in $array2 . In this case, array_diff_uassoc() thinks that the current key value pair in $array1 should be retained because it does not find the exact same key in $array2 . Therefore, these key-value pairs appear in the result array.
Example:
$array1 = [
'apple' => 'red',
'banana' => 'yellow'
];
$array2 = [
'apple' => 'green',
'cherry' => 'red'
];
function compare_keys($key1, $key2) {
return -1; // express key1 Less than key2
}
$result = array_diff_uassoc($array1, $array2, 'compare_keys');
print_r($result);
Output:
Array
(
[banana] => yellow
)
Explanation: Since compare_keys() returns -1 , indicating that the key apple is smaller than the key cherry , banana does not find the corresponding key pair in $array1 , so the key value pairs corresponding to banana will appear in the result array.
When key_compare_func returns 1 , it means that the key in $array1 is greater than the key in $array2 . At this point, array_diff_uassoc() considers the current key-value pair in $array1 to be redundant, so it will be excluded from the result array.
Example:
$array1 = [
'apple' => 'red',
'banana' => 'yellow'
];
$array2 = [
'apple' => 'green',
'banana' => 'yellow'
];
function compare_keys($key1, $key2) {
return 1; // express key1 Greater than key2
}
$result = array_diff_uassoc($array1, $array2, 'compare_keys');
print_r($result);
Output:
Array
(
[apple] => red
)
Explanation: In this example, compare_keys() returns 1 , indicating that the key apple is greater than the key banana . Therefore, the key-value pair of apple cannot be found in $array1 that is exactly the same key pair as $array2 , and it will appear in the returned array.
When using array_diff_uassoc() , the return values 0 , -1 and 1 of the callback function will directly affect the way the keys are compared:
Return 0 : means that the keys are equal, and the current key-value pair is not considered a difference.
Return -1 : means that the key in $array1 is smaller than the key in $array2 , and the current key-value pair will appear in the result array.
Return 1 : means that the key in $array1 is greater than the key in $array2 , and the current key-value pair will be excluded.
By adjusting the comparison logic of the callback function, we can flexibly control the comparison rules, thereby achieving customized differences comparison.