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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

array_fill(int $start_index, int $count, mixed $value): array

Parameters

ParameterDescription
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

Fill a listTry it yourself
<?php
print_r(array_fill(0, 3, "-"));

Output

Array
(
    [0] => -
    [1] => -
    [2] => -
)
A gridTry it yourself
<?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

VersionChange
PHP 4.2Added

In other languages

Was this page helpful?