How to Use array_filter in PHP

This article explains how the array_filter() function works in PHP to filter elements of an array. You will learn its syntax, how to use it with and without custom callback functions, and how to filter arrays based on keys or both keys and values.

What is array_filter()?

The array_filter() function in PHP filters the elements of an array using a callback function. It iterates over each value in the array, passing them to the callback. If the callback function returns true, the current value from the array is returned in the resulting array. Array keys are preserved during this process.

Syntax

array_filter(array $array, ?callable $callback = null, int $mode = 0): array

1. Using array_filter() Without a Callback

If you do not provide a callback function, PHP will automatically remove all “falsy” values from the array. This includes false, null, 0, 0.0, empty strings "", and empty arrays [].

$array = [1, 0, 2, null, 3, '', false, 4];

$result = array_filter($array);

print_r($result);

Output:

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

2. Using array_filter() With a Custom Callback

To filter values based on specific conditions, pass a custom callback function. The callback function must accept one parameter (the value of the array element) and return true or false.

Example: Filtering Even Numbers

$numbers = [1, 2, 3, 4, 5, 6];

$even_numbers = array_filter($numbers, function($value) {
    return $value % 2 === 0;
});

print_r($even_numbers);

Output:

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

3. Filtering by Keys or Both Keys and Values

By default, the callback function only receives the array values. By using the third argument ($mode), you can change this behavior.

Filtering by Key (ARRAY_FILTER_USE_KEY)

This mode passes only the array keys to the callback function.

$data = [
    'a' => 1,
    'b' => 2,
    'c' => 3,
    'd' => 4
];

// Keep only keys that match 'a' or 'c'
$result = array_filter($data, function($key) {
    return in_array($key, ['a', 'c']);
}, ARRAY_FILTER_USE_KEY);

print_r($result);

Output:

Array
(
    [a] => 1
    [c] => 3
)

Filtering by Both Key and Value (ARRAY_FILTER_USE_BOTH)

This mode passes both the value and the key to the callback function as arguments. Note that the value is passed as the first argument, and the key is passed as the second.

$data = [
    'apple' => 5,
    'banana' => 12,
    'cherry' => 3
];

// Filter where key length is greater than 5 and value is greater than 4
$result = array_filter($data, function($value, $key) {
    return strlen($key) > 5 && $value > 4;
}, ARRAY_FILTER_USE_BOTH);

print_r($result);

Output:

Array
(
    [banana] => 12
)

Resetting Array Keys

Because array_filter() preserves the original array keys, you may end up with non-sequential index numbers in a numeric array. To reset the keys back to sequential order starting from 0, wrap the result in array_values().

$numbers = [10, 21, 30, 43];

$even = array_filter($numbers, function($val) {
    return $val % 2 === 0;
});

// Reset indices
$reset_even = array_values($even);

print_r($reset_even);

Output:

Array
(
    [0] => 10
    [1] => 30
)