Current Location: Home> Latest Articles> What should I do if array_filter() does not filter as expected?

What should I do if array_filter() does not filter as expected?

M66 2025-06-05

Why doesn't array_filter() filtering take effect? Frequently Asked Questions and Solutions

In PHP, the array_filter() function is used to filter array elements through a callback function. When you use this function, it will iterate over the array and decide whether to retain the element based on the return value of the callback function. If the callback function returns true , the element will be retained; if false , the element will be filtered out.
However, sometimes array_filter() doesn't seem to work, causing the result to be inconsistent with expectations. Next, we will discuss some common problems and solutions.

1. The return value of the callback function does not meet expectations

The callback function used by array_filter() should return a boolean value. If the callback function returns null , false , or other false values, the elements will be filtered out. If true is returned, the element is preserved. If you find that array_filter() is not working as expected, it is very important to check the return value of the callback function.

Example question:

 $arr = [1, 2, 3, 4, 5];
$result = array_filter($arr, function($value) {
    if ($value % 2 == 0) {
        return 1;  // It should return here true,Instead 1
    }
    return false;
});
print_r($result);

Workaround: Make sure the callback function returns a boolean value true or false , not another value.

Corrected code:

 $arr = [1, 2, 3, 4, 5];
$result = array_filter($arr, function($value) {
    return $value % 2 == 0;  // Return a boolean value true or false
});
print_r($result);

2. The parameter type of the callback function is incorrect

If the expected parameter type of the callback function does not match the type of the actually passed array element, it may cause the elements to not be filtered correctly. For example, when the callback function expects to receive integer values, it is actually passing a string or other type of data.

Example question:

 $arr = ["1", "2", "3", "4", "5"];
$result = array_filter($arr, function($value) {
    return $value % 2 == 0;  // Here $value It's a string,Causing unexpected results
});
print_r($result);

Solution: Use casting or make sure that the element type of the array matches the callback function's requirements.

Corrected code:

 $arr = ["1", "2", "3", "4", "5"];
$result = array_filter($arr, function($value) {
    return (int)$value % 2 == 0;  // Cast to integer
});
print_r($result);

3. Processing of empty arrays or empty values

array_filter() will filter out all false values ​​in the array by default (such as null , false , empty strings, etc.). If there are null values ​​in the array, it may cause the filtering result to be worse than expected.

Example question:

 $arr = [0, 1, 2, null, false, "", 3];
$result = array_filter($arr);
print_r($result);  // It will be filtered out here 0、null、false and empty strings

Solution: If you want to keep values ​​like 0 or false , you can pass a custom callback function, or use the ARRAY_FILTER_USE_BOTH flag to control how to filter.

Code for custom callback function:

 $arr = [0, 1, 2, null, false, "", 3];
$result = array_filter($arr, function($value) {
    return $value !== null;  // reserve null Values ​​other than
});
print_r($result);

4. Modify the reference of the array

array_filter() returns a new array instead of modifying the original array. Modifying the original array requires reference passing or other methods. If you want to manipulate the original array directly, you can use array_walk() or other functions.

Example question:

 $arr = [1, 2, 3, 4, 5];
array_filter($arr, function($value) {
    return $value > 3;
});
print_r($arr);  // The original array has not been modified

Solution: If you want to modify the original array, you can use array_walk() or reassign the value directly.

Corrected code:

 $arr = [1, 2, 3, 4, 5];
$arr = array_filter($arr, function($value) {
    return $value > 3;
});
print_r($arr);  // Return the modified array directly

5. Misuse array_filter() parameters

array_filter() allows the second parameter to specify the filtering criteria. By default, it uses a callback function to filter elements. However, if this parameter is misused, it may also lead to problems.

Example question:

 $arr = [1, 2, 3, 4, 5];
$result = array_filter($arr, 1);  // Used the numeric value incorrectly 1
print_r($result);

Solution: Make sure the second parameter is a valid callback function.

Corrected code:

 $arr = [1, 2, 3, 4, 5];
$result = array_filter($arr, function($value) {
    return $value % 2 == 0;  // Use callback functions correctly
});
print_r($result);