In PHP's standard library functions, array_fill() is a small but very practical tool. During interviews, candidates are often examined whether they are familiar with this seemingly simple but ingenious function. Understanding and mastering the use of array_fill() can not only improve efficiency in actual development, but also demonstrate your proficiency in array processing in interviews.
array_fill() is used to fill an array with the specified value. The function prototype is as follows:
array array_fill ( int $start_index , int $count , mixed $value )
$start_index : Start index
$count : Number of elements
$value : The value used for filling
Example:
$array = array_fill(0, 5, 'PHP');
print_r($array);
Output:
Array
(
[0] => PHP
[1] => PHP
[2] => PHP
[3] => PHP
[4] => PHP
)
This function is very convenient when you need to quickly create an array with default values.
For example, you want to initialize a set of boolean arrays with a state of false :
$status = array_fill(0, 10, false);
Suitable for initialization tasks such as: user voting status, permission check mark, question answering status, etc.
Sometimes we process external data, some field values are missing, but we want to maintain a unified data format. At this time, you can use array_fill() to occupy the place.
$placeholders = array_fill(0, 3, 'N/A');
This writing is very common when exporting CSVs or building a unified data table.
Suppose a questionnaire has 5 questions and the default value for each question is 0 points, you can write it like this:
$scores = array_fill(1, 5, 0); // From the title number 1 start
Array_fill() combined with other array functions can implement more complex logic.
Joint with array_combine() :
If you have a set of field names, you want to assign a default value to each field:
$fields = ['name', 'email', 'phone'];
$defaults = array_fill(0, count($fields), '');
$data = array_combine($fields, $defaults);
Output:
Array
(
[name] =>
[email] =>
[phone] =>
)
This is an excellent way to handle form default values, interface field formatting.
array_fill() allows negative indexing, which is useful in some specific logic but requires careful handling.
$negArray = array_fill(-3, 3, 'X');
Output:
Array
(
[-3] => X
[-2] => X
[-1] => X
)
During the interview, the interviewer may ask questions like this:
Use array_fill() to quickly generate an array with indexes ranging from 1 to 100 and initial values of 0, requiring that the key of the final array be a continuous integer.
Answer:
$data = array_fill(1, 100, 0);
Advanced requirements may be: map all keys into string formats, such as 'item_1' to 'item_100' .
$keys = array_map(fn($n) => "item_$n", range(1, 100));
$values = array_fill(0, 100, 0);
$result = array_combine($keys, $values);
Suppose we want to build a POST interface and submit an array of 10 user default information to https://api.m66.net/data , each user data format is:
[
'name' => '',
'email' => '',
'status' => 'inactive'
]
We can write this way:
$defaultUser = [
'name' => '',
'email' => '',
'status' => 'inactive'
];
$users = array_fill(0, 10, $defaultUser);
$payload = json_encode(['users' => $users]);
$ch = curl_init('https://api.m66.net/data');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$response = curl_exec($ch);
curl_close($ch);
This code shows how array_fill() can simplify the process of building structured data in real business scenarios.
Mastering the use of array_fill() can make your code more concise and intuitive. Although it is not complicated, it often hides "the interviewer's test points". Next time you encounter a need for initialization and default value filling, don't forget its existence. Using array_fill() gracefully is not only a plus at the code level, but also a highlight of the details that stand out in the interview.