array_fill
array_fill
Fill an array with values
array **array_fill** int $start_index int $count mixed $value
Fills an array with
entries of the value of the
parameter, keys starting at the
parameter.
count``value``start_index
start_indexThe first index of the returned array.
If is negative,
the first index of the returned array will be
and the following
indices will start from zero prior to PHP 8.0.0;
as of PHP 8.0.0, negative keys are incremented normally
(see ).
`start_index``start_index`example
count
Number of elements to insert.
Must be greater than or equal to zero, and less than or equal to .
2147483647
valueValue to use for filling
Returns the filled array
Throws a if is
out of range.
ValueError``count
Voorbeeld: example
<?php
$a = array_fill(5, 6, 'banana');
print_r($a);
?>
Array
(
[5] => banana
[6] => banana
[7] => banana
[8] => banana
[9] => banana
[10] => banana
)
Voorbeeld: example with a negative start index
<?php
$a = array_fill(-2, 4, 'pear');
print_r($a);
?>
Array
(
[-2] => pear
[-1] => pear
[0] => pear
[1] => pear
)
Array
(
[-2] => pear
[0] => pear
[1] => pear
[2] => pear
)
Note that index is not present prior to PHP 8.0.0.
-1
See also the
section of manual for a detailed explanation of negative keys.
Arrays
array_fill_keys``str_repeat``range