Current Location: Home> Latest Articles> How to solve the problem of array_filter() failing to filter nested arrays

How to solve the problem of array_filter() failing to filter nested arrays

M66 2025-06-05

In PHP, array_filter() is a very common function that can be used to filter elements in an array. This function accepts an array and a callback function (if there is no callback function, the default Boolean transformation will be used), and returns a new array that contains only elements that meet the conditions. Although array_filter() is very effective when dealing with simple one-dimensional arrays, it seems out of place when we process nested arrays. So, why can't array_filter() filter nested arrays? How can we solve this problem? Let’s discuss in depth below.

Why can't array_filter() filter nested arrays?

The original intention of array_filter() is to filter one-dimensional arrays. It does not recursively process subarrays of an array. That is, array_filter() will only apply a callback function to the top-level elements of the array, and ignore the elements in the nested array completely. Therefore, if an array contains nested arrays, array_filter() cannot effectively filter elements in these nested arrays.

For example:

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

$result = array_filter($array, function($value) {
    return $value > 3;
});

print_r($result);

The output will be:

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

    [4] => Array
        (
            [0] => 6
            [1] => 7
        )
)

As you can see, nested arrays [3, 4] and [6, 7] are not filtered, but are kept in the result as is. This is because array_filter() does not recursively enter the nested array.

How to solve this problem and implement effective filtering?

The easiest solution to filter nested arrays is to use recursion. By calling array_filter() recursively, we can ensure that every element in the nested array can be processed.

Here is an implementation of recursive filtering:

 function array_filter_recursive($array, $callback) {
    return array_map(function($value) use ($callback) {
        if (is_array($value)) {
            return array_filter_recursive($value, $callback); // Recursively call to nested arrays
        } else {
            return $callback($value) ? $value : null; // Filter individual elements
        }
    }, $array);
}

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

$result = array_filter_recursive($array, function($value) {
    return $value > 3;
});

print_r($result);

The output result will be:

 Array
(
    [1] => 
    [2] => Array
        (
            [1] => 4
        )

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

As you can see, each element in the array is filtered and the elements in the nested array are filtered correctly.

explain:
  • array_filter_recursive() recursively traverses the array using array_map() , for each element, check if it is an array. If it is an array, array_filter_recursive() is applied recursively. If it is a single value, the callback function is applied to filter.

  • In this implementation, the null value is filtered out. You can adjust the behavior of the callback function according to actual needs and decide how to handle filtered elements.

in conclusion

array_filter() itself can only filter one-dimensional arrays and cannot handle nested arrays. By recursively calling array_filter() or similar custom functions, we can easily perform efficient filtering of nested arrays. Depending on your needs, you can also modify the logic of the callback function to make it more flexible and adapt to different scenarios.