Menu

PHP array_filter(): Filter an Array by Value or Key

array_filter($array, $callback) keeps the elements for which the callback returns true. Learn how to remove empty values, filter by key with ARRAY_FILTER_USE_KEY, reindex the result with array_values, and add things up with array_reduce.

This page includes runnable editors - edit, run, and see output instantly.

array_filter($array, $callback) returns the elements of $array for which $callback returns true (or any value that converts to true). The array comes first, the callback second, and the elements that stay keep their original keys.

The keys in the result are 1, 3, 5, 7: the positions those values had in the original array. That detail matters more than it looks, and the section after next shows how to deal with it.

Remove empty values from an array

Call array_filter without a callback and it removes every value that converts to false: empty strings, null, false, 0, "0" and empty arrays. That is the quickest way to clean up the pieces of an explode() or a list of form fields.

The first result also lost "0" and 0, which are often real data (a quantity of zero, a postcode digit). When zero is a valid answer, write the callback yourself, as in the second call. The empty page has the full list of values PHP treats as empty.

Reindex the result with array_values

Because the keys are kept, a filtered list can have gaps. A for loop from 0 then hits missing keys, and json_encode turns the result into a JSON object instead of an array. array_values() renumbers it.

Filter by key with ARRAY_FILTER_USE_KEY

The third argument chooses what the callback receives. ARRAY_FILTER_USE_KEY passes only the key. ARRAY_FILTER_USE_BOTH passes the value and then the key.

To keep a fixed set of keys you already know, array_intersect_key($config, array_flip(['db_host', 'db_port'])) does the same without a callback.

Filter a multidimensional array

Most real data is a list of rows. The callback receives one row at a time, so you test whichever field you need.

Change $maxPrice and run it again. To get just the names afterwards, pass the result to array_column: array_column($affordable, 'name').

Filter, then map, then reduce

array_filter picks elements, array_map changes them, and array_reduce folds the array into one value. array_reduce($array, $callback, $initial) calls the callback with the running total and the next element.

For a plain sum, array_sum($totals) is shorter. array_reduce earns its place when the result is something else: a string, a count per group, the largest row.

Common mistake: changing values inside array_filter

The callback's return value only decides whether an element stays. Anything else it computes is thrown away, so this code does not trim anything:

Transform with array_map, then filter, and add array_values when you need a clean list.

Frequently Asked Questions

How do I remove empty values from an array in PHP?

Call array_filter($array) with no callback: it drops every value that is falsy ("", null, false, 0, "0", []). To drop only empty strings and null but keep 0, pass a callback: array_filter($array, fn($v) => $v !== '' && $v !== null).

Why does array_filter keep the old keys?

array_filter never renumbers: the elements it keeps stay under their original keys, so a list can come back as [0 => 'a', 2 => 'c']. Wrap it in array_values() to get 0, 1, 2 again, which also makes json_encode produce a JSON array instead of an object.

How do I filter an array by key in PHP?

Pass ARRAY_FILTER_USE_KEY as the third argument, and the callback receives the key instead of the value: array_filter($arr, fn($k) => str_starts_with($k, 'db_'), ARRAY_FILTER_USE_KEY). Use ARRAY_FILTER_USE_BOTH to receive the value and the key.

What is the difference between array_filter and array_map?

array_filter decides which elements stay and returns them unchanged. array_map changes every element and returns the same number of elements. To do both, chain them: array_map('strtoupper', array_filter($arr)).

Coddy programming languages illustration

Learn to code with Coddy

GET STARTED