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_filter

Trieda

Koreň \ Bez triedy

Metóda - array_filter

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

Tests each item in the input array against a given callback. If the callback returns true, the item will be present in array returned by this function, false otherwise. Array keys are preserved.

Caution: Modification of the input array from the callback (eg. adding, deleting or unsetting items) is classified as unexpected behavior. This usage of the callback is discouraged.

Procedurálne

Parametre

NázovDátový typPredvolená hodnotaPopis
$arrayarray

Array, whose items will be tested against a given callback.

$flagint 0

Callback flag.

  • ARRAY_FILTER_U­SE_KEY constant: Only one parameter will be passed to the callback - item value.
  • ARRAY_FILTER_U­SE_BOTH constant: Two parameters will be passed to the callback - item key and value.

Mávratovej hodnoty

Vracia: array

Returns array with items, against which the callback returned true.

Príklady

This example filters out every item, that is considered empty by the empty() function.

<?php function filterEmpty($x) {
    return !empty($x);
}

$array = array(false, null, 0, '0', '', 'NonEmptyValue', 'NonEmptyValue2'); $filteredArray = array_filter($array, filterEmpty); var_dump($filteredArray);

Súvisiace manuály

      • function array_map (callable $callback, array $array1, array $...) : array
      • function array_reduce (array $array, callable $callback) : mixed
      Aktivity