During the web development process, we often need to filter the data submitted by the form so that only input items that meet certain specific conditions are retained. PHP provides a very useful function array_filter() , which can not only be used to remove null values in arrays, but also customize callback functions to implement more complex filtering logic.
This article will introduce how to use array_filter() to conditionally filter the data submitted in the form to help you write more concise and maintainable PHP code.
array_filter() is an array processing function provided by PHP. Its syntax is as follows:
array_filter(array $array, ?callable $callback = null, int $mode = 0): array
$array : The array to be filtered.
$callback : (optional) A callback function to define filter conditions.
$mode : (Optional) You can use ARRAY_FILTER_USE_KEY or ARRAY_FILTER_USE_BOTH to specify the callback function parameters.
If the callback function is not passed, array_filter() will automatically remove elements with false in the array (such as false , 0 , "" , null , [] , etc.).
Suppose we have a user registration form that submits the following fields:
Name
age
Whether to subscribe to email
We want to filter out fields with values filled in and only keep user data older than 18 years old.
$_POST = [
'name' => 'Alice',
'email' => '',
'age' => 17,
'subscribe' => 'yes'
];
// Custom filter criteria
$filteredInput = array_filter($_POST, function ($value, $key) {
// Skip empty values
if ($value === '' || $value === null) {
return false;
}
// against age Fields are processed in specific ways
if ($key === 'age') {
return (int)$value > 18;
}
return true;
}, ARRAY_FILTER_USE_BOTH);
print_r($filteredInput);
Array
(
[name] => Alice
[subscribe] => yes
)
As you can see, email is empty strings that are culled, age less than or equal to 18 is also filtered out, and the rest are valid fields that we care about.
You can also use this method to clean GET parameters from URL requests, for example:
$_GET = [
'search' => 'PHP',
'page' => '0',
'sort' => null,
'token' => 'abc123'
];
$cleanQuery = array_filter($_GET, function ($value) {
return $value !== '' && $value !== null;
});
$queryString = http_build_query($cleanQuery);
$url = "https://m66.net/search.php?" . $queryString;
echo $url;
https://m66.net/search.php?search=PHP&page=0&token=abc123
Note that the page value of '0' is not filtered because we explicitly exclude the judgment of 0 and retain meaningful parameters.
Using array_filter() can greatly simplify our code logic when processing form input, and is especially suitable for lightweight data verification or preprocessing. When used with callback functions, it can meet the needs of most conditional filtering and is a very practical function in PHP.
It should be noted that array_filter() is not a universal tool for data verification. For more complex verification needs, it is still recommended to combine PHP's filter extensions (such as filter_var() ) or use a complete verification framework for processing.
I hope you can make good use of array_filter() to improve development efficiency when processing form data next time you process it!