Current Location: Home> Latest Articles> Is array_filter() more efficient to replace foreach?

Is array_filter() more efficient to replace foreach?

M66 2025-06-03

In PHP development, we often need to filter out elements that meet certain conditions from arrays. One of the most common ways is to use a foreach loop with conditional statements for filtering. However, the PHP standard library also provides a more concise function - array_filter() , which is designed for this requirement. Then the question is:

1. Comparison of basic usage

Suppose we have an array of user lists and we want to filter out users older than 18 years old.

How to use foreach:

 $users = [
    ['name' => 'Alice', 'age' => 22],
    ['name' => 'Bob', 'age' => 17],
    ['name' => 'Charlie', 'age' => 30],
];

$adults = [];
foreach ($users as $user) {
    if ($user['age'] > 18) {
        $adults[] = $user;
    }
}

How to use array_filter:

 $users = [
    ['name' => 'Alice', 'age' => 22],
    ['name' => 'Bob', 'age' => 17],
    ['name' => 'Charlie', 'age' => 30],
];

$adults = array_filter($users, function($user) {
    return $user['age'] > 18;
});

In terms of code readability, array_filter() is more compact and clear, especially when logic is simple.

2. Performance comparison

Let's compare the performance differences between the two with a large amount of data through a simple test script.

 $users = [];
for ($i = 0; $i < 100000; $i++) {
    $users[] = ['name' => "User$i", 'age' => rand(10, 50)];
}

// use foreach
$start = microtime(true);
$adults1 = [];
foreach ($users as $user) {
    if ($user['age'] > 18) {
        $adults1[] = $user;
    }
}
$time1 = microtime(true) - $start;

// use array_filter
$start = microtime(true);
$adults2 = array_filter($users, function($user) {
    return $user['age'] > 18;
});
$time2 = microtime(true) - $start;

echo "foreach time consuming: {$time1} Second\n";
echo "array_filter time consuming: {$time2} Second\n";

Example of test results (may vary by device):

 foreach time consuming: 0.025 Second
array_filter time consuming: 0.027 Second

As can be seen, the performance difference is minimal , and even in some cases foreach is slightly faster, because array_filter() is essentially also using loops internally and also involves the overhead of a function call.

3. Suggestions for use

  • Recommended scenarios where array_filter() is used:

    • The conditions are simple and the code is clearer.

    • Want to write code in a functional style.

    • The data processing chain needs to be used with other array functions (such as array_map() ).

  • Recommended scenarios foreach:

    • Need for higher performance and ultimate optimization.

    • Conditional logic is complex or requires interruption of loops.

    • Additional operations are required to do the original array (such as reference assignment, etc.).

4. Summary

While foreach may be slightly better in some cases, array_filter() provides a more elegant solution, especially when pursuing code readability and maintainability. In terms of performance, the difference between the two is minimal, and the choice should be based on the actual scenario rather than blindly pursuing "efficiency" .

If you want the code to be easier to understand and maintain, you might as well try array_filter() !