PHP array_uintersect()Functions
Examples
Compare two arrays'key values(using a user-defined function to compare key values), and return the intersection:
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);
Run example »
Definition and Usage
The array_uintersect() function is used to compare two (or more) arrays'key valuesand return 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 the key values in the compared array (array1), and also in any other parameter array (array2orarray3etc.).
Syntax
array_uintersect(array1,array2,array3...,myfunction)
| Parameter | Description |
|---|---|
| array1 | Required. The first array to compare with other arrays. |
| array2 | Required. The array to compare with the first array. |
| array3,... | Optional. Other arrays to compare with the first array. |
| myfunction | Required. A string defining a callable comparison function. If the first argument is <, =, > the second argument, the comparison function must return an integer <, =, > 0 respectively. |
Technical Details
| Return value: | Returns an intersection array that includes all the key values in the compared array (array1), and also in any other parameter array (array2orarray3etc.). |
|---|---|
| PHP Version: | 5+ |
More Examples
Example 1
Use the built-in strcasecmp function to perform case-insensitive comparison, and return the intersection:
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "GREEN", "B" => "brown", "yellow", "red");
print_r(array_uintersect($array1, $array2, "strcasecmp"));
Run example »
Complete PHP Array Reference Other Extensions