In daily PHP development, especially when processing forms or batch data submissions, we often encounter the need to initialize and filter a large number of input values. This article will introduce how to use the two functions of array_fill() and array_filter() to implement initialization and validity filtering of batch input values.
array_fill(int $start_index, int $count, mixed $value): array
Used to create an array and fill it with the specified value, suitable for initializing a fixed structure data collection.
array_filter(array $array, ?callable $callback = null, int $mode = 0): array
Used to filter values in an array. By default, all elements equivalent to false (such as 0 , false , null , '' , etc.) in the array will be removed, and more complex filtering logic can be implemented through custom callback functions.
Suppose we are working on a batch form submission with 10 entries. Each entry is a name field entered by the user. We hope:
Initialize these entries as default empty strings;
Filter out unfiltered items, and only items with names filled in are retained for next processing or storage.
<?php
// Assume that the user submitted data is as follows(Partially empty)
$submittedData = [
'Alice',
'',
'Bob',
null,
'Charlie',
'',
' ',
'David',
false,
'Eve'
];
// first step:initialization 10 entries,The default value is an empty string(Prevent data incomplete when not submitted)
$inputCount = 10;
$defaultData = array_fill(0, $inputCount, '');
// Step 2:将用户提交的数据合并到initialization数组中,Avoid index missing
$mergedData = array_replace($defaultData, $submittedData);
// Step 3:use array_filter Filter invalid data(Like an empty string、null、false、Spaces, etc.)
$validData = array_filter($mergedData, function ($value) {
// use trim Remove spaces,Then determine whether it is empty
return trim($value) !== '';
});
// Step 4:Process valid data(If saved、verify、Output)
foreach ($validData as $index => $name) {
echo "1. " . ($index + 1) . " Item valid input:{$name}\n";
}
1. 1 Item valid input:Alice
1. 3 Item valid input:Bob
1. 5 Item valid input:Charlie
1. 8 Item valid input:David
1. 10 Item valid input:Eve
This combination is very useful in practical applications, such as:
Multi-line message board;
Bulk user import;
Set parameters in batches (such as URL arrays, API request data, etc.);
Preprocess the batch fields uploaded by users (such as title, description, etc. when uploading batches).
For example, you may need to get a set of URL addresses from the form and verify that it is not empty and valid:
$urls = [
'https://m66.net/api/user',
'',
'https://m66.net/docs',
null,
' '
];
$urls = array_filter($urls, function ($url) {
return trim($url) !== '';
});
// Output有效链接
print_r($urls);
By using array_fill() to initialize the data structure and cleaning the user-submitted data with array_filter() , the processing flow of batch data in PHP can be greatly simplified. This method is not only efficient and concise, but also improves the robustness and maintainability of the code. It is a skill that every PHP developer should master.
If you often process batch data input from forms, APIs, or databases, try using these two functions to optimize your processing logic.