PHPArrayarray_filter

Filters elements of an array using a callback function.

Syntax

array_filter(array $array, ?callable $callback): array

Example

Enter values below to update the example in real time.

nums
evens
array_filter
fn
print_r
array_values
$nums = [1, 2, 3, 4, 5, 6];
$evens = array_filter($nums, fn($n) => $n % 2 === 0);
print_r(array_values($evens)); // [2, 4, 6]