In PHP, array_filter is a very practical function to filter arrays based on the return result of the callback function. By default, if the callback function is not provided, array_filter removes all elements considered "falsy values". These "false values" include:
false
null
0 (integer)
'0' (string)
'' (empty string)
[] (Empty array)
This may confuse some developers, especially when they originally wanted to remove null or empty strings, they found that even the integer 0 was deleted together. So, why is this happening? Let's take a deeper look.
PHP is a weak type language, which means that when it performs a Boolean operation, it automatically converts certain types to Boolean values. This mechanism is called Type Juggling. In a Boolean context, the following values are interpreted as false :
var_dump((bool) false); // false
var_dump((bool) null); // false
var_dump((bool) 0); // false
var_dump((bool) '0'); // false
var_dump((bool) ''); // false
var_dump((bool) []); // false
As long as a value is converted to a Boolean type and false , array_filter will filter it by default.
The array_filter function prototype is as follows:
array_filter(array $array, ?callable $callback = null, int $mode = 0): array
When the second parameter $callback is null , array_filter actually works as follows:
$result = [];
foreach ($array as $key => $value) {
if ((bool) $value) {
$result[$key] = $value;
}
}
For example:
$data = [0, 1, 2, null, '', 'hello', false];
$result = array_filter($data);
// turn out [1 => 1, 2 => 2, 5 => 'hello']
As you can see, 0 , null , '' and false are all considered "false" and are therefore removed.
If you just want to filter out null values and keep 0 and empty strings, you can pass in a custom callback function, for example:
$data = [0, '0', '', null, false, 'PHP'];
$result = array_filter($data, function ($item) {
return !is_null($item);
});
// Keep it 0、'0' and ''
For example, if you just want to remove empty strings:
$result = array_filter($data, function ($item) {
return $item !== '';
});
This allows you to carefully control which values need to be retained according to specific needs.
Suppose you are building a form submission system, and the front-end form may pass back some empty fields, and you want to keep only what the user actually fills in:
$formData = [
'name' => 'Alice',
'email' => '',
'age' => 0,
'website' => null,
];
$filtered = array_filter($formData, function ($value) {
return $value !== '' && !is_null($value);
});
print_r($filtered);
Output result:
Array
(
[name] => Alice
[age] => 0
)
In this result, the empty string and null are removed, while 0 is retained because the user may have actually filled in the age of 0.
When using array_filter , it is crucial to understand the concept of "false values" in PHP. By default, it removes all values equivalent to boolean false , not just null or empty strings. If you need more precise filtering behavior, it is recommended to always pass in a custom callback function.
In this way, you can avoid accidentally deleting valid data, such as the "0" value that the user does fill in, and you can also flexibly deal with various business scenarios.