Current Location: Home> Latest Articles> Does it make sense to fill an array with null with array_fill()?

Does it make sense to fill an array with null with array_fill()?

M66 2025-06-05

In PHP, array_fill() is a very practical function that can quickly generate arrays of preset sizes and contents. But there is a seemingly strange usage, which is to use null as the padding value to initialize the array. Is this practical significance? Is it necessary to use it in development? Let’s discuss it together.

1. Basic usage of array_fill()

Let’s first look at the basic syntax of array_fill() :

 array_fill(int $start_index, int $count, mixed $value): array

This function will generate $count elements starting from the specified $start_index , and the value of each element is $value . For example:

 $filled = array_fill(0, 5, null);
print_r($filled);

Output:

 Array
(
    [0] => 
    [1] => 
    [2] => 
    [3] => 
    [4] => 
)

In this example, we get an array containing 5 null values.

2. Why use null to fill an array?

On the surface, filling null values ​​doesn't seem to make sense, but it's actually very useful in some scenarios:

1. Pre-initialize the array structure

Sometimes we want to "occupy" an array first, and then fill in the specific value according to the logic later. for example:

 $results = array_fill(0, 10, null);

foreach ($data as $item) {
    $index = $item['position'];
    $results[$index] = $item['value'];
}

In this case, $results guarantees a fixed length from the beginning, and each position is null by default. This avoids errors or warnings caused by accessing undefined indexes.

2. Maintain index consistency

On some front-end pages, such as tables, charts, or paging systems, we want all data structures to be complete, even if some locations do not have values. This way, the returned JSON structure is more regular. For example:

 $data = array_fill(1, 12, null); // 1 The month is here 12 The default value of the month

// Assume that there are only 2 moon、3 moon和 7 moon的数据
$data[2] = 150;
$data[3] = 180;
$data[7] = 90;

echo json_encode($data);

Output:

 {
    "1": null,
    "2": 150,
    "3": 180,
    "4": null,
    "5": null,
    "6": null,
    "7": 90,
    "8": null,
    "9": null,
    "10": null,
    "11": null,
    "12": null
}

This structure can reduce the processing of judgment logic and boundary situations during front-end processing.

3. Performance optimization and clear intention expression

Using array_fill() allows the code to express the intent of "I need an array of N elements, each element takes up a place before processing". This method is more intuitive and more efficient than manually writing loops to fill null or empty strings.

 // Traditional writing
for ($i = 0; $i < 10; $i++) {
    $arr[$i] = null;
}

// Recommended writing
$arr = array_fill(0, 10, null);

3. Use scenarios in actual development

In actual projects, here are a few examples of this approach that may be used:

  • In the data reporting system, a data framework used to initialize month/quarter/category;

  • When processing distributed tasks, pre-allocated result placeholder arrays;

  • Used to generate a default configuration structure to facilitate subsequent merging with user input;

  • Before rendering the template, initialize the data placeholder to prevent the template from reporting errors.

For example, you build a chart analysis system on m66.net , and the user requests the monthly sales data of a certain product. Even if there is no sales in a certain month, he still hopes that the returned data structure is complete. At this time, you can use array_fill() to complete the preset:

 $monthlySales = array_fill(1, 12, null);
// Query the database and populate it
foreach ($dbResults as $row) {
    $monthlySales[$row['month']] = $row['sales'];
}

4. Conclusion: It is necessary, but there must be "intention"

Although not every project will use array_fill() to fill null values, it is a good tool when you are facing requirements such as data alignment, preset structure, formatting, and result placeholding.

The key is not "whether to use it", but "whether you know why you want to use it." As long as it is to clearly express your development intentions and improve the readability and robustness of the code, it is necessary.