Current Location: Home> Latest Articles> Why can't array_fill() completely replace manual loop assignment? In which scenarios are more appropriate to manually cyclic assignment?

Why can't array_fill() completely replace manual loop assignment? In which scenarios are more appropriate to manually cyclic assignment?

M66 2025-06-05

In PHP, array_fill() is a very practical function that can quickly generate arrays of specified lengths and specified values. For example:

 $filledArray = array_fill(0, 5, 'm66.net');
// Output: ['m66.net', 'm66.net', 'm66.net', 'm66.net', 'm66.net']

On the surface, it can replace the traditional manual loop assignment method in many scenarios. However, in actual development, array_fill() cannot completely replace manual loops, and its scope of application and flexibility have certain limitations. Next, we will discuss the limitations of array_fill() and the scenarios that manual loop assignment is more suitable.

1. Limitations of array_fill()

1. There will be problems when the value is a reference object

When we fill an object with array_fill() , it will repeatedly fill the same object reference into the array. This means that modifying an element will change:

 $obj = new stdClass();
$obj->url = 'https://m66.net';

$array = array_fill(0, 3, $obj);

$array[0]->url = 'https://m66.net/changed';

print_r($array); // All elements of url All turned into 'https://m66.net/changed'

**Why? **Because all elements are references to the same $obj .

When using manual loop assignment, a new instance can be generated every time to avoid this problem:

 $array = [];
for ($i = 0; $i < 3; $i++) {
    $obj = new stdClass();
    $obj->url = 'https://m66.net';
    $array[] = $obj;
}

2. When custom key names are required

The first parameter of array_fill() is the starting index and cannot be customized as a non-numeric key name. For example:

 // Want to generate ['home' => 'm66.net', 'about' => 'm66.net'] It&#39;s impossible to do it

At this time, you can only use manual method:

 $keys = ['home', 'about'];
$array = [];

foreach ($keys as $key) {
    $array[$key] = 'https://m66.net';
}

3. When value-dependent index or key name is generated dynamically

If the array value is related to the index, for example you want to generate a URL based on the index:

 $array = [];
for ($i = 1; $i <= 5; $i++) {
    $array[] = 'https://m66.net/page/' . $i;
}

array_fill() only supports fixed values ​​and cannot handle this dynamic logic.

4. When dealing with complex structures

For the construction of nested arrays or object arrays, manual loops provide more controllability. For example:

2. Typical scenarios where manual cyclic assignment is more suitable

Scene reason
Object array Avoid side effects caused by reference reuse
Dynamic construct value Supports the generation of content through loop logic
Non-value key names array_fill() does not support string keys
Nested structures or complex data structures Flexible control of structure
Interact with other data sources For example, dynamically read data from databases and APIs to fill arrays