PHP 8 reference · Array Functions
PHP array_fill() Function
Create an array filled with the same value, given a start index and a count.
PHP 8 Since 4.2
Syntax
array_fill(int $start_index, int $count, mixed $value): array
Parameters
| Parameter | Description |
|---|---|
start_index |
The first key. |
count |
How many elements; must be 0 or more. |
value |
The value for every element. |
Return value
The filled array.
Examples
<?php
print_r(array_fill(0, 3, "-"));
Output
Array
(
[0] => -
[1] => -
[2] => -
)<?php
$board = array_fill(0, 3, array_fill(0, 3, "."));
$board[1][1] = "X";
foreach ($board as $row) {
echo implode(" ", $row), "\n";
}
Output
. . . . X . . . .
Version history
| Version | Change |
|---|---|
| PHP 4.2 | Added |
In other languages
array.fill()· JavaScript
Related
array_fill_keys()· Create an array from a list of keys, giving every key the same value. Useful for defaults and counters.range()· Create an array of numbers or letters from a start to an end value, with an optional step, such as range(1, 10, 2).array_pad()· Pad an array to a given length with a value, adding at the end (positive length) or the start (negative).str_repeat()· Repeat a string a given number of times and return the result. A count of 0 returns an empty string.- PHP Arrays · tutorial
- PHP Array Functions · tutorial