NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - array_intersect

Trieda

Koreň \ Bez triedy

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ázovDátový typPredvolená hodnotaPopis
$array1array

The array with values to intersect.

$array2array

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.

Súvisiace manuály

        Aktivity