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 (array1en ook in elk ander parametersarray (array2 of array3 enz.) van de sleutels.
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 compare with the first array. |
myfunction |
Required. String value, defining 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
Use a user-defined callback function myfunction to calculate the intersection of two or more arrays (that is array1 All array elements that exist simultaneously in other arrays as well)
Only compare key values, not key names, such as "a"=>1 and "b"=>1 are considered equal.
myfunction De door de parameter opgegeven functie wordt gebruikt om te vergelijken of elementen gelijk zijn.myfunction De functie heeft twee parameters die worden vergeleken. Als de eerste parameter kleiner is dan de tweede parameter, retourneert de functie een negatief getal, als de parameters gelijk zijn, retourneert de functie 0, en als de eerste parameter groter is dan de tweede, retourneert de functie een positief getal.
De array waarin de sleutelnamen ongewijzigd blijven.
Technische Details
Retourneren: |
retourneert een array die alle in array1 bevatten, evenals in alle andere arrays. retourneert een intersection array die alle elementen bevat die in alle te vergelijken arrays voorkomen (array1en ook in elk ander parametersarray (array2 of array3 enz.) van de sleutels. |
PHP Versie: | 5+ |
Meer voorbeelden
Voorbeeld 1
Vergelijk de sleutels van drie arrays (gebruik een gebruikersdefineerde functie om sleutels te vergelijken) en retourneer het 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); ?>