PHP array_udiff_assoc() function
Example
Compare the key names and values of two arrays (using built-in functions to compare key names and user-defined functions to compare values), and return the difference set:
<?php function myfunction($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"blue","c"=>"green"); $result=array_udiff_assoc($a1,$a2,"myfunction"); print_r($result); ?>
Definition and Usage
The array_udiff() function is used to compare the key names and values of two (or more) arrays and returns the difference set.
Note:This function uses built-in functions to compare key names and user-defined functions to compare values.
This function compares the key names and values of two (or more) arrays and returns a difference set array that includes all elements in the compared arrays (array1in, but not in any other parameter array (array2 or array3 etc.) key names and values.
Description
The return value of the array_udiff_assoc() function array1 Parts that exist in one array but not in others.
注意与 array_diff() array_diff() and array_udiff()
the difference is that the key name is also used for comparison. Key names and values are compared simultaneously. For example, "a"=>1 and "b"=>1 are two elements that are not equal. Array data comparison is performed using the callback function provided by the user. In this respect, and array_diff_assoc()
The behavior of array_udiff_assoc() function is exactly opposite, the latter is compared with the internal function. myfunction The parameter specified function is used to compare whether the elements are equal.myfunction The function has two parameters to be compared. If the first parameter is less than the second parameter, the function returns a negative number, if the two parameters are equal, it should return 0, and if the first parameter is greater than the second, it returns a positive number.
Syntax
array_udiff_assoc(array1,array2,array3,...myfunction)
Parameters | Description |
---|---|
array1 | Required. The first array to be compared with the other arrays. |
array2 | Required. The array to be compared with the first array. |
array3,... | Optional. Other arrays to compare with the first array. |
myfunction |
Required. String value, defining the callable comparison function. If the first parameter is less than or equal to or greater than the second parameter, the comparison function must return an integer less than or equal to or greater than 0. |
Technical Details
Return Value: | Returns an array difference, which includes all elements in the compared arrays (array1in, but not in any other parameter array (array2 or array3 etc.) key names and values. |
PHP Version: | 5+ |