← All built-ins / array_filter()

array_filter()

array_filter(...$args): mixed

Parameters

NameTypeDescription
$aarrayarray to filter
$fnclosurepredicate; 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

See also

array_map()preg_grep()