PHP array_diff_ukey() function
Example
Compare the keys of two arrays (using a user-defined function to compare keys) and return the difference set:
<?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" => "blue", "b" => "black", "e" => "blue"); $result=array_diff_ukey($a1, $a2, "myfunction"); print_r($result); ?>
Definition and Usage
The array_diff_ukey() function is used to compare the keys of two (or more) arrays and returns the difference set.
Note:This function uses a user-defined function to compare keys!
This function compares the keys of two (or more) arrays and returns a difference set array that includes all elements that are present in the compared arrays (array1) but not in any other parameter array (array2 or array3 etc.) key names.
Syntax
array_diff_ukey(array1,array2,array3...,myfunction);
Parameters | Description |
---|---|
array1 | Mandatory. The first array to compare with other arrays. |
array2 | Mandatory. The array to compare with the first array. |
array3,... | Optional. Another array to compare with the first array. |
myfunction | Mandatory. A string that defines a callable comparison function. The comparison function must return an integer less than, equal to, or greater than 0 if the first argument is less than, equal to, or greater than the second argument. |
Description
array_diff_ukey() returns an array that includes all the keys that appear in array1 but not appearing in any other parameter array, the value of the key name. Note that the association is retained unchanged. Unlike array_diff(), the comparison is based on key names rather than values.
This comparison is done through a callback function provided by the user. An integer less than, equal to, or greater than zero must be returned when the first parameter is considered to be less than, equal to, or greater than the second parameter.
Technical Details
Return Value: | Returns the difference set array, which includes all the keys in the compared arrays (array1) but not in any other parameter array (array2 or array3 etc.) key names. |
PHP Version: | 5.1+ |
More Examples
Example 1
Compare the key names of three arrays (using a user-defined function to compare key names) and return the difference set:
<?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"=>"black","b"=>"yellow","d"=>"brown"); $a3=array("e"=>"purple","f"=>"white","a"=>"gold"); $result=array_diff_ukey($a1,$a2,$a3,"myfunction"); print_r($result); ?>