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