Compare the keys and values of two arrays using two user-defined functions for comparison, and return the matching entries.
<?php function myfunction_value($a,$b) { if ($a===$b) { return 0; } return ($a>$b)?1:-1; } function myfunction_key($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”,“c”=>“green”); $result=array_uintersect_uassoc($a1,$a2,“myfunction_value”,“myfunction_key”); print_r($result); ?> |
The array_uintersect_uassoc() function compares the keys and values of two or more arrays and returns the matches.
Note: It uses two user-defined functions for comparison—one for the values and another for the keys.
This function checks both the keys and values of the arrays and returns an array containing entries from the first array that are present in the second, third, and any additional arrays.
array_uintersect_uassoc(array1, array2, array3, …, myfunc_value, myfunc_key) |
Parameter |
Description |
array1 |
Required. The array to use for comparison. |
array2 |
Required. An array to compare with. |
array3,… |
Optional. Additional arrays to compare with. |
myfunc_value |
Required. The name of the user-defined function for comparing array values. This function must be callable and return an integer less than, equal to, or greater than 0, indicating whether the first argument is less than, equal to, or greater than the second argument. |
myfunc_key |
Required. The name of the user-defined function for comparing array keys. This function must be callable and return an integer less than, equal to, or greater than 0, depending on whether the first argument is less than, equal to, or greater than the second argument. |
Return Value: |
Returns an array with entries from array1 that are found in all the other specified arrays. |
PHP Version: |
5+ |