PHP array_uintersect_assoc() Function
Example
Compare the keys and values of two arrays (using built-in functions to compare keys and user-defined functions to compare values) and return the intersection:
<?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_uintersect_assoc($a1,$a2,"myfunction"); print_r($result); ?>
Definition and Usage
The array_uintersect_assoc() function is used to compare the keys and values of two (or more) arrays and returns the intersection (matches).
Note:This function uses the built-in function to compare keys and a user-defined function to compare values!
This function compares the keys and values of two (or more) arrays and returns an intersection array that includes all elements present in the compared arrays (array1), and also in any other parameter array (array2 or array3 etc.) key names and key values.
Note that with array_uintersect() The difference is that the key names are also compared. Data (key values) are compared using callback functions.
Syntax
array_uintersect_assoc(array1,array2,array3...myfunction)
Parameters | Description |
---|---|
array1 | Required. The first array to be compared with 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, defines 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. |
Description
Using a user-defined callback function function to calculate the intersection of two or more arrays (i.e., array1 that exist in both the array and any other array, and returns the resulting array.
Both key names and key values are compared at the same time, such as the two elements "a"=>1 and "b"=>1 are not equal.
function The parameter specifies the function used to compare whether elements are equal.function The function takes two parameters to compare. 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.
The array returned maintains the same key names.
Technical Details
Return Value: | Returns an array containing all elements that are present in array1 is also present in all other arrays. |
PHP Version: | 5+ |