PHP array_uintersect() function
Example
Compare the key values of two arrays (using a user-defined function to compare key values) 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" => "blue", "b" => "black", "e" => "blue"); $result=array_uintersect($a1, $a2, "myfunction"); print_r($result); ?>
Definition and Usage
The array_uintersect() function is used to compare the key values of two (or more) arrays and returns the intersection.
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 an intersection array that includes all elements in the compared arrays (array1) and also in any other parameter array (array2 or array3 etc.) key values.
Syntax
array_uintersect(array1,array2,array3...myfunction)
Parameters | Description |
---|---|
array1 | Required. The first array to be compared with other arrays. |
array2 | Required. The array to be compared with the first array. |
array3,... | Optional. Other arrays to be compared with the first array. |
myfunction |
Required. A string value that 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. |
Description
using a user-defined callback function myfunction to calculate the intersection of two or more arrays (i.e., array1 All array elements that exist in the array and also in any other array are returned in the result array.
Only the comparison of key values is performed, not the key names, such as "a"=>1 and "b"=>1 are considered equal.
myfunction The function specified by the parameter is used to compare whether elements are equal.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, return 0; if the first parameter is greater than the second, return a positive number.
The key names in the returned array remain unchanged.
Technical Details
Return Value: |
Returns an array containing all array1 in all other arrays at the same time. Returns an intersection array that includes all units that are in all the compared arrays (array1) and also in any other parameter array (array2 or array3 etc.) key values. |
PHP Version: | 5+ |
More Examples
Example 1
Compare the key values of three arrays (using a user-defined function to compare key values) 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","yellow"); $a2=array("A"=>"red","b"=>"GREEN","yellow","black"); $a3=array("a"=>"green","b"=>"red","yellow","black"); $result=array_uintersect($a1,$a2,$a3,"myfunction"); print_r($result); ?>