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 (array1enthält, sowie in jedem anderen Parameterarray (array2 oder array3 u.a.) Schlüsselwerte.
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 simultaneously in any other array), and return 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 Die vom Parameter angegebene Funktion wird verwendet, um zu vergleichen, ob Elemente gleich sind.myfunction Die Funktion hat zwei Parameter, die verglichen werden sollen. Gibt der erste Parameter einen kleineren Wert als der zweite Parameter zurück, gibt die Funktion einen negativen Wert zurück, gibt sie 0 zurück, wenn die beiden Parameter gleich sind, und gibt sie einen positiven Wert zurück, wenn der erste Parameter einen größeren Wert als der zweite Parameter hat.
Die Schlüsselnamen im zurückgegebenen Array bleiben unverändert.
Technische Details
Rückgabewert: |
Gibt ein Array zurück, das alle in array1 enthält, sowie in allen anderen Arrays. Gibt ein Schnittmenge-Array zurück, das alle Elemente enthält, die in allen verglichenen Arrays (array1enthält, sowie in jedem anderen Parameterarray (array2 oder array3 u.a.) Schlüsselwerte. |
PHP-Version: | 5+ |
Mehr Beispiele
Beispiel 1
Vergleichen Sie die Schlüsselwerte von drei Arrays (verwenden Sie eine vom Benutzer definierte Funktion zum Vergleichen von Schlüsselwerten) und geben Sie das Schnittmenge zurück:
<?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); ?>