Current Location: Home> Latest Articles> array_filter() Performance analysis: callback vs default behavior

array_filter() Performance analysis: callback vs default behavior

M66 2025-05-17

In PHP, array_filter() is a very commonly used array processing function, mainly used to filter elements in an array. This function can receive an optional callback function that defines which elements should be retained. If no callback function is provided, array_filter() will automatically remove elements in the array with "false values" such as false , null , 0 , empty string and empty array.

So the question is: In actual use, what is the performance difference between using array_filter() with default behavior and providing custom callback functions? Which method is more efficient?

This article will conduct in-depth analysis from the performance perspective by comparing and testing the execution time of these two usages.

1. Review of basic grammar

 // Default behavior:Remove the array“Fake value”
$result = array_filter($array);

// Use callback functions:Only retain greater than 0 Value of
$result = array_filter($array, function($value) {
    return $value > 0;
});

2. Test code examples

We construct an array with a large number of elements and filter them using default behavior and custom callback functions respectively.

 function testArrayFilterDefault($array) {
    $start = microtime(true);
    $result = array_filter($array);
    return microtime(true) - $start;
}

function testArrayFilterCallback($array) {
    $start = microtime(true);
    $result = array_filter($array, function($value) {
        return $value > 0;
    });
    return microtime(true) - $start;
}

// Construct a test array,Include 100000 Elements
$testArray = [];
for ($i = 0; $i < 100000; $i++) {
    $testArray[] = $i % 2 === 0 ? 0 : $i; // Half is 0,Half is整数
}

// Perform a test
$timeDefault = testArrayFilterDefault($testArray);
$timeCallback = testArrayFilterCallback($testArray);

echo "Default behavior执行时间:{$timeDefault} Second\n";
echo "Callback function execution time:{$timeCallback} Second\n";

3. Performance analysis results

Run the script above and you will find that the default behavior executes faster in most cases. The reasons are as follows:

  • The default behavior is built-in logic implemented by the C language layer, and the execution efficiency is higher.

  • When using callback functions, PHP requires calling user-defined functions every time, and the overhead of function calling cannot be ignored, especially when large amounts of data.

Sample output (slightly different depending on the specific machine):

 Default behavior执行时间:0.012345 Second
Callback function execution time:0.035678 Second

As you can see, the default behavior is about two to three times faster .

4. Should the default behavior always be used?

Although the default behavior is better, it has limitations - only "false values" can be removed. If your filtering logic is a little complicated, such as retaining a specific range of values, type checks, etc., you still need a callback function.

For example:

 // Keep odd values
array_filter($array, function($v) {
    return $v % 2 !== 0;
});

This obviously cannot be achieved through default behavior.

5. Real case: Filter the form data submitted by users

 $userInput = [
    'name' => 'Alice',
    'email' => '',
    'age' => 0,
    'newsletter' => false,
];

$cleanedInput = array_filter($userInput); // 只保留非Fake value字段

// Output ['name' => 'Alice']

In this example, if you want to remove all unfilled fields (including false , empty strings, etc.), the default behavior is very appropriate. And if you want to only preserve non-empty string fields, you need to use a callback:

 $cleanedInput = array_filter($userInput, function($value) {
    return is_string($value) && trim($value) !== '';
});

6. Summary

How to use performance flexibility Use scenarios
Default behavior ? Efficient ? Restricted Remove false values
Use callback functions ? Slightly slow ? Flexible Custom filtering logic

Suggestion: If you just remove false values, use the default behavior; if more complex logic is required, it is inevitable to use callback functions.