PHP - array_uintersect
Trieda
Metóda - array_uintersect
(PHP 5, PHP 7)
The function compares the values of at least two arrays according to a user-defined comparison function and returns their intersection.
Procedurálne
- function array_uintersect (array $array1, array $array2, array $..., callable $value_compare_func) : array
- function callback () : array
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $array1 | array | The main array, which is compared to the following arrays. | |
| $array2 | array | An array which is compared to the first one. | |
| $... | array | More arrays which are compared to the first one. | |
| $value_compare_func | callable | Specifies a callable comparison function. The function must return a negative integer if the first argument of the function is less than the second argument, a positive integer if it's greater or zero if they're equal. Note: If a compared value is a string, the comparison is case sensitive. |
Mávratovej hodnoty
Vracia: array
The function returns an array containing elements from the first array which values are present in all of the other arrays.
Príklady
The function compares values of arrays and therefore there is no difference if we use an indexed or an associative array.
<?php
// The comparison function:
function compare_values($value1, $value2) {
if ($value1 === $value2) {
return 0;
}
return ($value1 > $value2) ? 1 : -1;
}
$seminar1 = array("Terminator", "Rambo", "McCane", "Tarzan", "Batman");
$seminar2 = array("Superman", "Norris", "Rambo", "Hulk", "Batman");
$seminar3 = array("Rambo", "Tarzan", "batman", "Norris");
$result = array_uintersect($seminar1, $seminar2, $seminar3, "compare_values");
// The only student in the result is Rambo.
// Batman is in the last array with small "b", therefore he is not in the result.
print_r($result);
We modify our comparison function, so the comparison will be case insensitive.
<?php
function compare_values_2($value1, $value2) {
if (strtolower($value1) == strtolower($value2)) {
return 0;
}
return (strtolower($value1) > strtolower($value2)) ? 1 : -1;
}
$seminar1 = array("Terminator", "Rambo", "McCane", "Tarzan", "Batman");
$seminar2 = array("Superman", "Norris", "Rambo", "Hulk", "Batman");
$seminar3 = array("Rambo", "Tarzan", "batman", "Norris");
$result = array_uintersect($seminar1, $seminar2, $seminar3, "compare_values_2");
// Now in the result is also Batman.
print_r($result);
Súvisiace manuály
- function array_intersect (array $array1, array $array2, array $...) : array
