The array_fill function simply fills an array with the provided values. This function is useful especially when we need to initialize an array with some dummy data.
What is the syntax of the array_fill function in PHP?
array_fill(index, number, value)
Parameters | Details |
---|---|
index | Index of the array to start inserting the value at – required |
number | Specify the number of elements to insert – required |
value | Specify the value to insert – required |
Example of array_fill function
<?php
$a1=array_fill(3,4,"blue");
print_r($a1);
?>
In the above example, the array_fill function returns an array containing “blue” at the index numbers 3, 4, 5, and 6. This is because we specified the string to start appearing at index 3 and repeat four times.