array_fill()
array_fill() — Fill an array with values.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Fills an array with count entries of the value of the value parameter, keys starting at the start_index parameter.
Signature
array_fill(int $start_index, int $count, mixed $value): array
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $start_index | int | The first index of the returned array. |
| $count | int | Number of elements to insert. Must be greater than or equal to zero, and less than or equal to 2147483647. |
| $value | mixed | Value to use for filling |
Return value
Returns the filled array
Example
A runnable snippet:
$a = array_fill(5, 6, 'banana'); print_r($a);
Output:
Array ( [5] => banana [6] => banana [7] => banana [8] => banana [9] => banana [10] => banana )
Changelog
| Version | Change |
|---|---|
| 8.0.0 | array_fill() now throws a ValueError if count is out of range; previously E_WARNING was raised, and the function returned false. |
See also
array_fill_keys(), str_repeat(), range()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page array_fill() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.