English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
array_diff_uassoc()function compares the keys and values of two (or more) arrays and returns an array1array, which contains entries that do not exist in any other array with the same values.
This function is different fromarray_diff()because array_diff() compares values, while this function compares keys and values from other arrays.
This function is similar toarray_diff_assoc() differs from array_diff_assoc() because array_diff_assoc() uses an internal algorithm to compare keys and their values, while this function uses a user-defined function to compare keys and their values.
array_diff_uassoc ( $array1, $array2 , $array3..., callback $key_compare_func]);
Serial Number | Parameters and Description |
---|---|
1 | array1(Required) The array to be compared |
2 | array2(Required) This is the array to be compared with the first array |
3 | array3(Optional) The array to be compared with the first array |
4 | key_compare_func(Required) The comparison function must return an integer less than, equal to, or greater than 0 when the first parameter is less than, equal to, or greater than the second parameter. |
This function returns an array that includes array1in all entries, but does not exist in any other array.
This function was first introduced in PHP version5.0.0 introduced.
Try the following example. Here, if $input1If the key is equal to any other input array, the key comparison function returns 0; If larger, return1; If smaller, return -1.
When comparing keys with a defined function, there is indeed a key "a" in both arrays, so it will not appear in the output. The next key "b" and "c" are not in the second array, so they will enter the output. Another 0 => "red" is in the output because the key in the second parameter "red" is1:
<?php function key_compare_func($a, $b) { if ($a === $b) { return 0; } return ($a > $b)? 1: -1; } $input1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $input2 = array("a" => "green", "yellow", "red"); $result = array_diff_uassoc($input1, $input2, "key_compare_func"); print_r($result); ?>Test to see‹/›
Output Result:
Array ( [b] => brown [c] => blue [0] => red )
Try the following example. This time, "red" will not be in the output because both keys are now equal to 0.
<?php function key_compare_func($a, $b) { if ($a === $b) { return 0; } return ($a > $b)? 1: -1; } $input1 = array("a" => "green", "b" => "brown", "c" => "blue", "red"); $input2 = array("a" => "green", "c" => "yellow", "red"); $result = array_diff_uassoc($input1, $input2, "key_compare_func"); print_r($result); ?>Test to see‹/›
Output Result:
Array ( [b] => brown [c] => blue )