PHP array_filter() Function | PHP Array Functions

The array_filter() function filters elements of an array using a callback function. It removes elements for which the callback function returns false.

Key Features of array_filter()

Syntax of array_filter()

Syntax

array array_filter(array $array, callable|null $callback = null, int $mode = 0);

Example Using array_filter()

The following example demonstrates how array_filter() is used:

Example


$array = [1, 2, 3, 4, 5];
        
$filtered = array_filter($array, function($value)
{
return $value % 2 === 0;
});
        
print_r($filtered);

Output

Array ( [1] => 2 [3] => 4 )