Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

array_fill()

Initialize consecutive integer keys from a chosen starting index, assigning the same value at each key.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Initialize consecutive integer keys from a chosen starting index, assigning the same value at each key. A count of zero produces an empty array. In PHP 8 the count must be between 0 and 2147483647 inclusive or a ValueError is raised. Filling with an object points all slots to that same object, so a property change is visible through every slot.

Signature

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

Example output

{"4":{"n":8},"5":{"n":8}}

Examples

<?php
$state = (object)['n'=>0];
$slots = array_fill(4, 2, $state);
$slots[4]->n = 8;
echo json_encode($slots);

Result

{"4":{"n":8},"5":{"n":8}}

Sources