Current Location: Home> Latest Articles> Use array_filter() to filter arrays in arrays in combination with is_array()

Use array_filter() to filter arrays in arrays in combination with is_array()

M66 2025-06-04

How to use array_filter() in combination with is_array() function to filter array elements in an array?

In PHP programming, we often need to manipulate arrays, especially when dealing with multidimensional arrays. Sometimes we may need to filter out all subarray elements from a multidimensional array. array_filter() is a very useful function that can be used to filter array elements, while is_array() function is used to check whether a variable is an array. If we use these two functions together, we can easily filter out all array elements from the array.

Article content

In this article, we will learn how to combine is_array() and array_filter() functions to filter array elements in an array. With practical examples, you will be able to better understand how these two functions are used.

1. Introduction to array_filter() function

The array_filter() function is used to filter elements in an array. It accepts an array and decides whether to keep elements in the array based on the boolean value returned by the callback function. The element that returns true will be retained by the callback function, and the element that returns false will be filtered out.

 $array = [1, 2, 3, 4, 5];
$filteredArray = array_filter($array, function($value) {
    return $value > 3; // Only retain greater than 3 Elements
});
print_r($filteredArray);

Output:

 Array
(
    [3] => 4
    [4] => 5
)

2. Introduction to is_array() function

The is_array() function is used to check whether the given variable is an array. If it is an array, return true , otherwise false .

 $var = [1, 2, 3];
if (is_array($var)) {
    echo "This is an array";
} else {
    echo "This is not an array";
}

Output:

 This is an array

3. Combining is_array() and array_filter() to filter array elements in an array

Now, suppose we have an array containing multiple subarrays and non-array elements. If we want to filter out all subarray elements from it, we can use is_array() to determine whether each element is an array, and combine array_filter() to filter out elements that meet the conditions.

For example, the following is a mixed array containing array and non-array elements:

 $array = [
    1,
    [2, 3, 4],
    "hello",
    [5, 6, 7],
    8,
    "world"
];

We want to filter out all subarray elements in it. To do this, we can use the array_filter() function and filter the array elements in the array through is_array() .

 $array = [
    1,
    [2, 3, 4],
    "hello",
    [5, 6, 7],
    8,
    "world"
];

$filteredArray = array_filter($array, function($value) {
    return is_array($value); // 仅保留数组类型Elements
});

print_r($filteredArray);

Output:

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

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

As shown above, array_filter() uses is_array() to determine whether each element is an array through the callback function, and retains elements of the array type. Non-array elements are filtered out.

4. Summary

By combining is_array() and array_filter() , we can easily filter out all array elements from an array of mixed types. This method is especially suitable for processing multi-dimensional arrays, which can quickly filter out subarrays to help us perform further processing.

The above is an introduction and sample code on how to use array_filter() in combination with the is_array() function to filter array elements in an array. Hope it helps you!