The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value.
array_fill($start_index, $number_elements, $values)
The array_fill() function takes three parameters and are described below:
- $start_index: This parameter specifies the starting position of filling up the values into the array, the user wants to create. If $start_index is negative, the first index of the returned array will be $start_index and the following indices will start from zero. So it is better to assign a positive value to it. This is a mandatory parameter and must be supplied.
- $number_elements: This parameter refers to the number of elements, the user wants to enter into the array. The $number_elements should be positive (including 0, for ver 5.6.0) otherwise E_WARNING is thrown. This is also a mandatory parameter.
- $values : This parameter refers to the values we want to insert into the array. These values can be of any type.
Return Type: The array_fill() function returns a filled user-defined array, with values described by $value parameter.
Input : $start_index = 2; $number_elements = 3; $values = "Geeks"; Output : Array (  => Geeks  => Geeks  => Geeks ) Input : $start_index = -10; $number_elements = 3; $values = 45; Output : Array ( [-10] => 45  => 45  => 45 )
Below program illustrates the working of array_fill() function in PHP:
Array (  => Geeks  => Geeks  => Geeks  => Geeks  => Geeks )