PHP 4.0.0 and above
The array_fill function is used to fill an array. It will fill the specified range of an array with the given value, and both the starting index and the array size can be customized.
array_fill(int $start_index, int $num, mixed $value): array
Returns a filled array, the starting index of the array is the specified value, the length is the specified number, and the value of each element is the provided fill value.
<?php // 创建一个从索引5开始,包含10个元素,且所有元素的值为“apple”的数组 $result = array_fill(5, 10, "apple"); <p>// Output result<br> print_r($result);<br> ?><br>
The above code creates an array starting from index 5 and fills with "apple" 10 elements starting from index 5.
In the above example, `array_fill(5, 10, "apple")` means that starting from the index 5 of the array, filling 10 elements, all elements have values "apple". The final output array will start from index 5 and contain 10 "apple" elements:
Array ( [5] => apple [6] => apple [7] => apple [8] => apple [9] => apple [10] => apple [11] => apple [12] => apple [13] => apple [14] => apple )