PHP array_intersect_uassoc() function
Example
Compare the key names and key values of two arrays (use a user-defined function to compare key names) 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("d" => "red", "b" => "green", "e" => "blue"); $result=array_intersect_uassoc($a1, $a2, "myfunction"); print_r($result); ?>
Definition and Usage
The array_intersect_uassoc() function is used to compare the key names and key values of two (or more) arrays and return the intersection.
Note:This function uses a user-defined function to compare key names!
This function compares the key names and key values of two (or more) arrays and returns an intersection array that includes all the elements in the compared arrays (array1)are also in any other parameter arrays (array2 or array3 etc.) key names and values.
Description
The array_intersect_uassoc() function uses a user-defined callback function to calculate the intersection of arrays, using the callback function to compare indices.
array_intersect_uassoc() returns an array that contains all the values array1 also appear in all other parameter arrays. The key names in the returned array remain unchanged.
Note that, unlike array_intersect(), it also compares key names in addition to key values.
This comparison is performed through a callback function provided by the user. The function takes two parameters, which are the key names to be compared. If the first parameter is less than the second, the function should return a negative number; if the two parameters are equal, it should return 0; if the first parameter is greater than the second, it should return a positive number.
syntax
array_intersect_uassoc(array1,array2,array3...myfunction)
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 | Required. A string that defines 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 the intersection array, which includes all key names and values from the arrays being compared (array1)are also in any other parameter arrays (array2 or array3 etc.) key names and values. |
PHP Version: | 5+ |
More Examples
Example 1
Compare the key names and values of three arrays (using a user-defined function to compare key names) 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"=>"green","d"=>"blue"); $a3=array("e"=>"yellow","a"=>"red","d"=>"blue"); $result=array_intersect_uassoc($a1,$a2,$a3,"myfunction"); print_r($result); ?>