In PHP, array manipulation is a very common programming task. Whether working with data or performing complex calculations, arrays are one of the core data structures. Filling arrays is a frequent requirement, and PHP provides multiple ways to do it, with the two most common methods being the array_fill() function and manually filling arrays using a for loop.
Each method has its pros and cons. This article will compare the two from a performance perspective to help developers choose the most suitable approach.
array_fill() is a built-in PHP function used to fill an array with a specified range of elements. Its function signature is as follows:
<span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-title function_ invoke__">array_fill</span></span><span>(</span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$start_index</span></span><span>, </span><span><span class="hljs-keyword">int</span></span><span> </span><span><span class="hljs-variable">$num</span></span><span>, </span><span><span class="hljs-keyword">mixed</span></span><span> </span><span><span class="hljs-variable">$value</span></span><span>)
</span></span>
$start_index: The starting index of the array.
$num: The number of elements to fill.
$value: The value to assign to each element.
array_fill() fills the array starting at the specified index with the specified number of elements, each having the same $value.
For example, to create an array with 10 elements, each set to 0:
<span><span><span class="hljs-variable">$arr</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_fill</span></span><span>(</span><span><span class="hljs-number">0</span></span><span>, </span><span><span class="hljs-number">10</span></span><span>, </span><span><span class="hljs-number">0</span></span><span>);
</span></span>
In this example, array $arr is filled from index 0 with 10 elements, each having the value 0.
Another common way to fill an array is by using a for loop to assign values manually. This allows developers to have more control over the filling process. A typical example is:
<span><span><span class="hljs-variable">$arr</span></span><span> = [];
</span><span><span class="hljs-keyword">for</span></span><span> (</span><span><span class="hljs-variable">$i</span></span><span> = </span><span><span class="hljs-number">0</span></span><span>; </span><span><span class="hljs-variable">$i</span></span><span> < </span><span><span class="hljs-number">10</span></span><span>; </span><span><span class="hljs-variable">$i</span></span><span>++) {
</span><span><span class="hljs-variable">$arr</span></span><span>[</span><span><span class="hljs-variable">$i</span></span><span>] = </span><span><span class="hljs-number">0</span></span><span>;
}
</span></span>
Here, a for loop starts from index 0, filling each position in the array with the value 0. Unlike array_fill(), a for loop offers more flexibility, allowing for more complex operations during the filling process.
When comparing array_fill() and for loops in terms of performance, we need to consider several factors, including execution speed, memory usage, and code maintainability. Let’s break it down.
In terms of raw performance, array_fill() is generally faster than a for loop. The reason is simple: array_fill() is a built-in, highly optimized function that operates directly at the memory level. A for loop, on the other hand, requires extra operations on each iteration, such as index access and assignment, which are relatively slower.
We can verify this with a simple performance test:
<span><span><span class="hljs-comment">// Using array_fill</span></span><span>
</span><span><span class="hljs-variable">$start</span></span><span> = </span><span><span class="hljs-title function_ invoke__">microtime</span></span><span>(</span><span><span class="hljs-literal">true</span></span><span>);
</span><span><span class="hljs-variable">$arr1</span></span><span> = </span><span><span class="hljs-title function_ invoke__">array_fill</span></span><span>(</span><span><span class="hljs-number">0</span></span>, <span class="hljs-number">1000000</span>, <span class="hljs-number">0</span>);
</span><span><span class="hljs-variable">$end</span></span> = microtime(true);
</span><span>echo "array_fill execution time: " . ($end - $start) . " seconds\n";
<p></span>// Using for loop<br>
$start = microtime(true);<br>
$arr2 = [];<br>
for ($i = 0; $i < 1000000; $i++) {<br>
$arr2[$i] = 0;<br>
}<br>
$end = microtime(true);<br>
echo "for loop execution time: " . ($end - $start) . " seconds\n";<br>
When filling large amounts of data, array_fill() generally takes less time than a for loop because it is optimized at the core level, whereas the for loop requires extra index lookups and assignments.
In terms of memory, there’s usually little difference between the two. Both array_fill() and a for loop allocate memory to store the filled elements. While array_fill() may have some internal optimizations for memory handling, the impact is minimal, especially with larger arrays.
However, if the for loop includes more complex operations (e.g., calculations for each element), additional memory usage may occur. Therefore, array_fill() is more suitable for simple fill operations, while for loops are better for complex scenarios.
From a readability and maintainability standpoint, array_fill() has the advantage. Its syntax is concise and clearly expresses the intention of filling an array, without relying on loop constructs. For simple tasks, using array_fill() improves code clarity.
The for loop, while more flexible, is more verbose and requires explicitly managing indices and assignments. For complex array filling tasks, this flexibility may be necessary, but for simple fills, it can be unnecessarily cumbersome.
Use array_fill() when:
Use for loop when:
From a performance standpoint, array_fill() typically outperforms for loops, especially with large datasets, because it fills arrays more efficiently. However, for loops offer greater flexibility and are more suitable when complex logic is involved.
Ultimately, the choice depends on the specific use case. For simple array filling, array_fill() is the better choice. If more control and logic are required during the process, a for loop is the way to go.