PHP array_udiff() function
Example
Compare the key values of two arrays (using a user-defined function to compare key values) 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_udiff($a1, $a2, "myfunction"); print_r($result); ?>
Definition and Usage
The array_udiff() function is used to compare the key values of two (or more) arrays and returns the difference set.
Note:This function uses a user-defined function to compare key values!
This function compares the key values of two (or more) arrays and returns a difference array that includes all elements in the compared arrays (array1) but not in any other parameter array (array2 or array3 etc.) of the key values.
Description
array_udiff() function returns an array that includes all values in the compared arrays but not in any other parameter arrays, with the key names unchanged.
array_udiff() function and array_diff() function behaves differently, the latter uses an internal function for comparison.
Data comparison is done using the array_udiff() function's myfunction is performed.myfunction The function takes two parameters to be compared. 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.
syntax
array_udiff(array1,array2,array3...myfunction)
Parameters | Description |
---|---|
array1 | Required. The first array to be compared with the 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. |
Technical Details
Return Value: | Returns a difference set array that contains all the elements in the compared arrays (array1) but not in any other parameter array (array2 or array3 etc.) of the key values. |
PHP Version: | 5.1.0+ |
More Examples
Example 1
Compare the key values of three arrays (using a user-defined function to compare key values) 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","yellow"); $a2=array("A"=>"red","b"=>"GREEN","yellow","black"); $a3=array("a"=>"green","b"=>"red","yellow","black"); $result=array_udiff($a1,$a2,$a3,"myfunction"); print_r($result); ?>