PHP - array_filter
Trieda
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
- function array_filter (array $array, int $flag = 0) : array
Parametre
| Názov | Dátový typ | Predvolená hodnota | Popis |
|---|---|---|---|
| $array | array | Array, whose items will be tested against a given callback. | |
| $flag | int | 0 | Callback flag.
|
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
