array_fill
Creates an array of identical values.
Syntax
array_fill(int $start_index, int $count, mixed $value): arrayParameters
| Parameter | Type | Description |
|---|---|---|
| $start_index | int | The key of the first element. |
| $count | int | The number of elements. |
| $value | mixed | The value of every element. |
Return value
arrayThe filled array.
Examples
Five zeros:
<?php
echo implode(', ', array_fill(0, 5, 0));Output:
0, 0, 0, 0, 0Question marks for a database query:
<?php
$ids = [3, 7, 9];
$marks = implode(', ', array_fill(0, count($ids), '?'));
echo "WHERE id IN ($marks)";Output:
WHERE id IN (?, ?, ?)Notes
To fill an array from a list of keys, use array_fill_keys().
See also
- range — Creates an array holding a sequence of values.
- str_repeat — Repeats a string a given number of times.
In the tutorial
- Arrays — Lists and associative arrays in PHP — creating, changing, iterating and useful functions.