Current Location: Home> Latest Articles> Multi-condition filtering: How to implement multiple judgments in array_filter()

Multi-condition filtering: How to implement multiple judgments in array_filter()

M66 2025-06-03

In PHP, array_filter() is a very practical function that can be used to filter arrays based on a given callback function. Typically, it is used to delete elements in an array that do not meet the criteria. Today, we will dive into how to implement multiple filters using array_filter() in PHP.

array_filter() Basic usage

First, let's take a look at the basic usage of the array_filter() function. array_filter() receives two parameters: an array and a callback function. The callback function will judge each element in the array, and only elements that meet the conditions will be retained.

 <?php
$array = [1, 2, 3, 4, 5];
$result = array_filter($array, function($value) {
    return $value > 2;
});
print_r($result);
?>

The output result is:

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

In this example, array_filter() retains elements greater than 2 according to the judgment conditions of the callback function.

Implement multiple filtering conditions

If you want to implement multiple filtering conditions, such as filtering elements larger than 2 and are even numbers, we can use the && (and) logical operators in the callback function to combine multiple conditions.

 <?php
$array = [1, 2, 3, 4, 5, 6, 7, 8];
$result = array_filter($array, function($value) {
    return $value > 2 && $value % 2 === 0;
});
print_r($result);
?>

The output result is:

 Array
(
    [3] => 4
    [5] => 6
    [7] => 8
)

In this example, array_filter() filters out all elements that are greater than 2 and are even.

Multiple filters using custom functions

If your filter criteria are complex, or if you want to reuse the filter logic, you can encapsulate multiple filter criteria into a custom function.

 <?php
function filterConditions($value) {
    return $value > 2 && $value % 2 === 0 && $value < 8;
}

$array = [1, 2, 3, 4, 5, 6, 7, 8];
$result = array_filter($array, 'filterConditions');
print_r($result);
?>

The output result is:

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

In this example, we extract the filter logic into the filterConditions() function and call it in array_filter() . This not only makes the code clearer, but also facilitates logic reuse.

Combined with other array functions

Sometimes we may need to combine other array functions to complete more complex filtering operations. For example, suppose you need to filter out elements in an array that meet multiple criteria and return the filtered results in a specific order, you can first use array_map() to convert the array, and then use array_filter() to filter.

 <?php
$array = [1, 2, 3, 4, 5, 6, 7, 8];
$array = array_map(function($value) {
    return $value * 2;  // Multiply all elements by 2
}, $array);

$result = array_filter($array, function($value) {
    return $value > 5;  // Filter out greater than 5 Elements
});
print_r($result);
?>

The output result is:

 Array
(
    [2] => 6
    [4] => 10
    [6] => 14
    [7] => 16
)

In this example, we first use array_map() to convert elements in the array, and then use array_filter() to filter out elements greater than 5.

Summarize

array_filter() is a very powerful tool in PHP that can help us filter elements in an array based on specific conditions. When using array_filter() , we can combine multiple conditions and use custom functions to achieve more complex filtering requirements. In addition, array_filter() can also be used with other array functions, making it more flexible and efficient to process arrays.