PHP array_filter() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_filter() goes through an array and keeps only the items that pass a test. The test is a callback function that you write, and it returns true for items you want to keep.

You use array_filter() to select items, for example the even numbers, the users who are active or the strings that are not empty. If you leave out the callback, it removes all values that PHP treats as false, such as 0, an empty string, null and an empty array.

Syntax

Call array_filter() with the values you need. Start with this line.

PHP
$even = array_filter($numbers, fn($n) => $n % 2 === 0);
CallWhat it does
array_filter($numbers, fn($n) => $n > 2)Keeps only the numbers greater than 2.
array_filter($values)Removes every falsy value, such as 0, '', null and false.
array_filter($data, fn($k) => $k !== 'id', ARRAY_FILTER_USE_KEY)Filters by key instead of by value.

The keys of the kept items are preserved, so the result can have gaps. Use array_values() if you want a list that starts at 0 again. The original array is never changed.

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

Parameters

ParameterDescription
arrayRequired. The array to filter.
callbackOptional. A function that returns true to keep an item. If it is null or left out, all items that are falsy are removed.
modeOptional. 0 passes only the value to the callback (the default). ARRAY_FILTER_USE_KEY passes only the key. ARRAY_FILTER_USE_BOTH passes the value first and the key second.

Return Value

Return ValueDescription
arrayA new array with the items that passed the test, keeping their original keys.

PHP Examples

Example 1 — Keeping Even Numbers

Write a callback that returns true for the items you want to keep.

PHP
<?php
$numbers = [1, 2, 3, 4, 5, 6];

$even = array_filter($numbers, fn($n) => $n % 2 === 0);

print_r($even);

How It Works

  1. The callback is called once for every number.
  2. It returns true when the number divided by 2 leaves a remainder of 0.
  3. The numbers 2, 4 and 6 pass the test and are kept.
  4. Their original keys 1, 3 and 5 are preserved in the result.

Example 2 — No Callback: Removing Falsy Values

Without a callback, every value that PHP treats as false is removed, not just empty strings.

PHP
<?php
$values = [0, 1, '', null, 'a', false, [], '0', 2];

print_r(array_filter($values));

How It Works

  1. The values 0, '', null, false, [] and '0' are all falsy in PHP.
  2. All of them are removed.
  3. Only 1, 'a' and 2 remain.
  4. They keep their original keys 1, 4 and 8.

Example 3 — Filtering by Key or by Both

The third argument changes what the callback receives.

PHP
<?php
$prices = ['apple' => 3, 'banana' => 1, 'cherry' => 5];

$noBanana = array_filter($prices, fn($key) => $key !== 'banana', ARRAY_FILTER_USE_KEY);
echo implode(', ', array_keys($noBanana)), PHP_EOL;

$both = array_filter($prices, fn($value, $key) => $value > 2 && $key !== 'cherry', ARRAY_FILTER_USE_BOTH);
echo implode(', ', array_keys($both)), PHP_EOL;

How It Works

  1. ARRAY_FILTER_USE_KEY sends only the key to the callback.
  2. The key 'banana' is rejected, so 'apple' and 'cherry' remain.
  3. ARRAY_FILTER_USE_BOTH sends the value first and the key second.
  4. Only 'apple' has a value above 2 and a key that is not 'cherry'.

Frequently Asked Questions

It returns a new array that contains only the items for which a callback returns true. Items that fail the test are left out.
array_filter() removes every value that is falsy, which means 0, '0', an empty string, null, false and an empty array.
array_filter() keeps the original keys of the items it keeps. Wrap the result in array_values() to get consecutive keys that start at 0.
Pass ARRAY_FILTER_USE_KEY as the third argument. The callback then receives the key instead of the value. Use ARRAY_FILTER_USE_BOTH to receive both.
array_filter() chooses which items to keep and does not change them. array_map() changes every item and returns the same number of items.

Next: array_find()

array_find() returns the first value in an array for which a callback function returns tru.

array_find() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful