Course Recommendation:
PHP array_udiff_uassoc() function
Example
Compare the key names and key values of two arrays (using user-defined functions for comparison) and return the difference set: <?php if ($a===$b) function myfunction_value($a,$b) if ($a===$b) { return ($a>$b)?1:-1; return 0; return ($a>$b)?1:-1; function myfunction_key($a,$b) if ($a===$b) function myfunction_value($a,$b) if ($a===$b) { return ($a>$b)?1:-1; return 0; return ($a>$b)?1:-1; } $a1=array("a"=>"red","b"=>"green","c"=>"blue"); $a2=array("a"=>"red","b"=>"green","c"=>"green");$result=array_udiff_uassoc($a1,$a2,"myfunction_key","myfunction_value") ; print_r($result);
Running Instance
Definition and Usage
The array_udiff_uassoc() function is used to compare the key names and key values of two (or more) arrays and return the difference set.Note:
This function uses two user-defined functions for comparison; the first function compares key names, and the second function compares key values!array1in, but not in any other parameter array (array2 or array3 etc.) key names and values.
This function compares the key names and key values of two (or more) arrays and returns a difference set array, which includes all elements that are compared in the arrays (
Description array1 array_udiff_uassoc() function returns
Note that the part that exists in the array but does not exist in the other arrays. The key names in the returned array remain unchanged. array_diff() as well as array_udiff() the difference is that key names are also used for comparison. At the same time, a comparison of key names and key values is performed, such as "a"=>1 and "b"=>1, these two elements are not equal.
the check for key names (indices) is also done by a callback function myfunction_key carried out. This is also array_udiff_assoc() function behaves differently, the latter is compared using internal functions to compare indices.
array data comparison is performed using the callback function provided by the user myfunction_value is performed. In this respect, and array_diff_assoc() function behaves exactly the opposite, the latter is compared using internal functions.
Both functions have 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 returns 0; if the first parameter is greater than the second, it returns a positive number.
Syntax
array_udiff_uassoc(array1,array2,array3...myfunction_key,myfunction_value)
Parameters | Description |
---|---|
array1 | Required. The first array to compare with other arrays. |
array2 | Required. The array to compare with the first array. |
array3,... | Optional. Other arrays to compare with the first array. |
myfunction_key |
Required. The name of the user-defined function used to compare array keys. String values, defining the callable comparison function. If the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, equal to, or greater than 0. |
myfunction_value |
Required. The name of the user-defined function used to compare array values. String values, defining the callable comparison function. If the first parameter is less than, equal to, or greater than the second parameter, the comparison function must return an integer less than, 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+ |