In daily PHP development, we often need to set initial values for arrays, especially when processing form data, building interface response structures, or batch initializing data structures, manual assignment is not only lengthy, but also prone to errors. At this time, PHP's built-in array_fill() function is a very useful tool. It can help us quickly fill arrays, improve development efficiency, and make the code more readable and maintainable.
array_fill() is an array function provided by PHP to fill an array with the specified value.
array array_fill(int $start_index, int $count, mixed $value)
$start_index : The index of the first element in the array.
$count : How many elements to fill.
$value : The value used to fill.
This function returns an array of $count elements and starts with $start_index .
// Create a Container 5 Elements,All initial values are 0 Array of
$defaultArray = array_fill(0, 5, 0);
print_r($defaultArray);
Output:
Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
This method is particularly suitable for initializing scoring, status marking, permission bits and other structures.
We often need to prepare an initial structure for multiple users, products or modules. Array_fill() combined with array_map() or looping can simplify this process.
$userIds = [101, 102, 103];
// Generate an initial set of data for each user
$defaults = array_fill(0, count($userIds), [
'score' => 0,
'status' => 'pending',
'profile_url' => 'https://m66.net/user/profile'
]);
// use array_combine Translate the user ID Associated with the default value
$userData = array_combine($userIds, $defaults);
print_r($userData);
Output:
Array
(
[101] => Array
(
[score] => 0
[status] => pending
[profile_url] => https://m66.net/user/profile
)
[102] => Array
(
[score] => 0
[status] => pending
[profile_url] => https://m66.net/user/profile
)
[103] => Array
(
[score] => 0
[status] => pending
[profile_url] => https://m66.net/user/profile
)
)
This makes it easy to create a consistent initial data structure for multiple users without the need to assign values one by one.
If you want each structure to contain dynamic data, such as a different URL or identity, you can use array_map() to cooperate with the generation.
$userIds = [201, 202, 203];
$userData = array_map(function($id) {
return [
'score' => 0,
'status' => 'new',
'profile_url' => "https://m66.net/user/profile?id={$id}"
];
}, $userIds);
// To keep users ID As key name
$userData = array_combine($userIds, $userData);
print_r($userData);
In the output, each profile_url will automatically splice the user's ID, making the structure more flexible.
Initialize user settings;
Batch populate the default fields for interface response;
Generate test data or simulation data;
Create a state matrix or permission table.
array_fill() is a very practical function that has been ignored by many people. It can help us quickly and gracefully generate data with repeated structures, greatly simplifying data processing logic. When used with functions such as array_combine() and array_map() , building complex array structures also become easy to control. Next time you need to assign default values to a batch of data, don't forget to try this function.