In PHP development, arrays are very commonly used data structures. When operating arrays, we often need to filter the array, such as removing null values, retaining elements that meet certain conditions, etc. The array_filter() function is a very practical tool designed specifically for this.
This article will explain in detail the basic usage of array_filter() , the meaning of parameters and some common usage scenarios, to help you quickly master the practical application of this function.
array_filter() is one of the built-in functions of PHP. Its purpose is to use a callback function to filter each element in the array, leaving only elements with the return value true . Its syntax is as follows:
array_filter(array $array, ?callable $callback = null, int $mode = 0): array
$array : The original array to filter.
$callback (optional): A callback function used to determine whether an element is retained. If null , the default removal values are false , null , empty string '' , number 0 , empty array [] and other "false values".
$mode (PHP 7.4+): Specifies whether the content passed to the callback function is a value, key, or key-value pair.
This is the most common way to use it, suitable for cleaning form data or configuration items, etc.:
$data = ["apple", "", null, 0, "banana", false];
$filtered = array_filter($data);
print_r($filtered);
Output result:
Array
(
[0] => apple
[4] => banana
)
When no callback function is passed, array_filter() will remove "false value" by default.
For example, we only keep strings with length greater than 5 in the array:
$words = ["apple", "strawberry", "banana", "kiwi"];
$filtered = array_filter($words, function($word) {
return strlen($word) > 5;
});
print_r($filtered);
Output:
Array
(
[1] => strawberry
[2] => banana
)
Starting from PHP 7.4, you can control the filtering content through the third parameter $mode :
$data = [
'a' => 100,
'b' => 200,
'x' => 300,
];
$filtered = array_filter($data, function($key) {
return $key === 'x';
}, ARRAY_FILTER_USE_KEY);
print_r($filtered);
Output:
Array
(
[x] => 300
)
$_POST = [
'username' => 'alice',
'email' => '',
'age' => null,
];
$clean_data = array_filter($_POST);
This can quickly remove null fields and only valid data is retained.
$users = [
['name' => 'Tom', 'active' => true],
['name' => 'Jerry', 'active' => false],
['name' => 'Spike', 'active' => true],
];
$active_users = array_filter($users, function($user) {
return $user['active'];
});
print_r($active_users);
$urls = [
'https://m66.net/home',
'invalid-url',
'http://m66.net/contact',
'',
];
$valid_urls = array_filter($urls, function($url) {
return filter_var($url, FILTER_VALIDATE_URL);
});
print_r($valid_urls);
After using array_filter() , the key name of the original array will not be reset (if you need to reset it, you can use it with array_values() ).
If you need to filter by keyname, make sure PHP version >= 7.4 and use ARRAY_FILTER_USE_KEY .
The callback function should return true or false to determine whether the element is retained.
Related Tags:
array_filter