PHP array_filter() function


The array_filter() function in PHP is used to filter elements of an array based on a custom condition. It iterates over each element of the array and passes it to a callback function. If the callback returns true for an element, that element is included in the resulting array; otherwise, it is removed.

Syntax:

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

Parameters:

  • $array: The input array that will be filtered.
  • $callback (optional): A callback function that determines which elements are retained. The callback should return true for elements that should be kept and false for those that should be filtered out. If no callback is provided, only elements that are considered "truthy" (non-empty, non-null, non-false) are retained.
  • $mode (optional): Specifies what arguments are sent to the callback function. It can be one of the following:
    • ARRAY_FILTER_USE_KEY (to filter based on keys).
    • ARRAY_FILTER_USE_BOTH (to filter based on both keys and values).
    • The default is 0, which sends only the value to the callback.

Return Value:

  • The function returns a filtered array containing only elements that satisfy the condition in the callback function.

Example 1: Basic Usage with Callback

Here's an example where array_filter() is used to filter out all even numbers from an array:

<?php $array = [1, 2, 3, 4, 5, 6]; $filtered = array_filter($array, function($value) { return $value % 2 !== 0; // Keep only odd numbers }); print_r($filtered); ?>

Output:

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

In this example, the callback function checks if a value is odd (by checking if it's not divisible by 2). As a result, only odd numbers are retained in the filtered array.

Example 2: Using array_filter() Without a Callback

When no callback is provided, array_filter() removes all "falsy" values (i.e., 0, false, null, '', etc.).

<?php $array = [0, 1, false, 2, '', 3, null]; $filtered = array_filter($array); // Removes falsy values print_r($filtered); ?>

Output:

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

Here, array_filter() removes all falsy values (like 0, false, null, and '') from the array, leaving only truthy values.

Example 3: Filtering by Both Key and Value

You can pass both the key and value to the callback function by using the ARRAY_FILTER_USE_BOTH mode.

<?php $array = [ "a" => 10, "b" => 20, "c" => 30 ]; $filtered = array_filter($array, function($value, $key) { return $key === "a" || $value === 30; }, ARRAY_FILTER_USE_BOTH); print_r($filtered); ?>

Output:

Array ( [a] => 10 [c] => 30 )

In this case, the callback filters the array based on both keys and values, retaining elements where the key is "a" or the value is 30.

Example 4: Filtering by Key Only

You can filter the array based on its keys using ARRAY_FILTER_USE_KEY.

<?php $array = [ "apple" => 1, "banana" => 2, "cherry" => 3 ]; $filtered = array_filter($array, function($key) { return strpos($key, 'a') !== false; // Keep keys containing the letter 'a' }, ARRAY_FILTER_USE_KEY); print_r($filtered); ?>

Output:

Array ( [apple] => 1 [banana] => 2 )

Here, array_filter() filters the array based on keys containing the letter 'a'.

Practical Usage:

  • array_filter() is useful for:
    • Removing unwanted values from an array (e.g., empty values, zeros, false values).
    • Filtering arrays based on custom conditions (e.g., keeping only elements that meet certain criteria).
    • Working with associative arrays where both keys and values need to be filtered based on complex logic.

Summary:

  • array_filter($array, $callback) filters the input array based on a condition defined in a callback function.
  • The function returns a new array with only elements that meet the condition.
  • When no callback is provided, it removes falsy values from the array.
  • The $mode parameter allows filtering based on keys or both keys and values.

This function is powerful for refining arrays, making data processing and transformation more flexible.