← All built-ins / array_filter()
array_filter()
array_filter(...$args): mixed
Parameters
| Name | Type | Description |
|---|---|---|
| $a | array | array to filter |
| $fn | closure | predicate; without it, truthy elements are keptoptional |
Return value
The elements where $fn returned true (keys preserved).
Example
$e = array_filter([1, 2, 3, 4], fn($x) => $x % 2 == 0);
echo implode(",", $e), "
";
echo implode(",", array_filter([0, 1, "", "a"])), "
";2,4 1,a