The PHP Array Filter Function: A Comprehensive Guide
The PHP Array Filter function is a built-in function that is used to filter elements of an array based on a specified condition. This function is particularly
array_filter() is a built-in PHP function that returns a new array containing only the elements of the original array that pass a test you define. You give it a callback — a function that returns true to keep an element and false to drop it — and array_filter() runs it against every element.
This page covers how the function works, all three of its modes (filtering by value, by key, or by both), the gotcha that trips up most beginners (preserved keys), and the shorthand for removing "empty" values.
How array_filter() Works
array_filter() walks through the array one element at a time and calls your callback on each one:
- If the callback returns a truthy value, the element is kept.
- If it returns a falsy value, the element is discarded.
The original array is never modified — array_filter() builds and returns a fresh array. By default the callback receives the element's value (not its key).
Syntax
array_filter(array $array, ?callable $callback = null, int $mode = 0): array$array— the array to filter.$callback— the test function. Optional: if omitted, every element equal to a falsy value (false,0,0.0,"","0",null,[]) is removed.$mode— controls what the callback receives. One of0(default, the value),ARRAY_FILTER_USE_KEY, orARRAY_FILTER_USE_BOTH. See Filtering by key.
Filtering with a Custom Callback
The most common use is filtering by a custom condition. Here we keep only the even numbers:
Output:
Array ( [1] => 2 [3] => 4 [5] => 6 )Notice the keys: the kept elements keep their original keys (1, 3, 5), they are not renumbered. This is the single biggest surprise with array_filter(). See Reindexing the result below for the fix.
In modern PHP you can write the callback as a concise arrow function:
$evenNumbers = array_filter($numbers, fn ($number) => $number % 2 === 0);Using a Built-in Function as the Callback
The callback can be any callable, including a built-in function passed by name. Here is_numeric keeps only the numeric values:
Output:
Array ( [0] => 1 [1] => 2 [3] => 4 [4] => 5 )The string '2' is kept because is_numeric('2') is true, while 'three' is dropped.
Removing Empty Values (No Callback)
Call array_filter() with only the array to strip out every falsy element — a quick way to clean up a list of optional inputs:
<?php
$values = [0, 1, 2, '', '0', 'hello', null, false, []];
print_r(array_filter($values));
?>Output:
Array ( [1] => 1 [2] => 2 [5] => hello )Be careful: this removes 0, '0', and '' because they are falsy. If a literal 0 or '0' is meaningful in your data, pass an explicit callback instead, for example fn ($v) => $v !== null.
Filtering by Key or by Both
By default the callback only sees the value. Pass the $mode argument to change that:
ARRAY_FILTER_USE_KEY passes the key to the callback instead of the value:
<?php
$data = ['a' => 1, 'b' => 2, 'ab' => 3];
// Keep only single-letter keys
$result = array_filter($data, fn ($key) => strlen($key) === 1, ARRAY_FILTER_USE_KEY);
print_r($result);
?>Output:
Array ( [a] => 1 [b] => 2 )ARRAY_FILTER_USE_BOTH passes both the value and the key (value first):
<?php
$data = ['a' => 1, 'b' => 2, 'c' => 3];
$result = array_filter($data, fn ($value, $key) => $value > 1 && $key !== 'c', ARRAY_FILTER_USE_BOTH);
print_r($result);
?>Output:
Array ( [b] => 2 )Reindexing the Result
Because array_filter() preserves keys, an indexed array can end up with gaps ([1], [3], [5]). When you need a clean, zero-based list — for example before JSON-encoding it as a JSON array — wrap the result in array_values():
<?php
$numbers = [1, 2, 3, 4, 5, 6];
$even = array_values(array_filter($numbers, fn ($n) => $n % 2 === 0));
print_r($even);
?>Output:
Array ( [0] => 2 [1] => 4 [2] => 6 )When to Use array_filter()
- Use
array_filter()when you want a subset of an array (drop the elements you don't want). - Use
array_map()when you want to transform every element but keep the same count. - Use
array_reduce()when you want to collapse an array into a single value.
filter, map, and reduce compose well together — filter first, then map the survivors.
Conclusion
array_filter() is the idiomatic way to extract a subset of an array in PHP. Remember its two key behaviors: the original array is left untouched, and the surviving elements keep their original keys (use array_values() to reindex). With the optional callback and the ARRAY_FILTER_USE_KEY / ARRAY_FILTER_USE_BOTH modes, it handles filtering by value, key, or both. For more on working with arrays, see the PHP Arrays overview.