PHP array_fill() Function
The PHP array_fill() function returns an array filled with specified value with specified number of times and the index of the array starts from specified index.
Syntax
array_fill(start_index, count, value)
Parameters
start_index |
Required. Specify the staring index of the returned array. |
count |
Required. Specify the number of elements inserted. |
value |
Required. Specify the value to be inserted. |
Return Value
Returns the filled array.
Exceptions
Throws E_WARNING, if count is less than zero.
Example:
The example below shows the usage of array_fill() function.
<?php $Arr1 = array_fill(10, 5, "A"); $Arr2 = array_fill(-2, 5, 10); print_r($Arr1); echo "\n"; print_r($Arr2); ?>
The output of the above code will be:
Array ( [10] => A [11] => A [12] => A [13] => A [14] => A ) Array ( [-2] => 10 [-1] => 10 [0] => 10 [1] => 10 [2] => 10 )
❮ PHP Array Reference