PHP - array_intersect
Trieda
Metóda - array_intersect
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
The function returns an array which is an intersection of the values of entered arrays.
In the first array, only the keys whose values are also present in the second array are kept. The second array can be understood as a model. Keys from the second array will never be in the result.
Procedurálne
- function array_intersect (array $array1, array $array2, array $...) : array
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $array1 | array | The array with values to intersect. | |
| $array2 | array | An array with values to intersect. | |
| $... | array | Other arrays with values to intersect. |
Mávratovej hodnoty
Vracia: array
An array with an intersection of values of both arrays.
Príklady
<?php
$array = ['yellow', 'black', 'purple', 'red'];
$allowed = ['yellow', 'red', 'green'];
$array = array_intersect($array, $allowed);
print_r($array);
We can see the keys (here indexes) are not changed. The example would work the same way for an associative array.
