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 )
- How to get the function name inside a function in PHP ?
- PHP | Ds\Set first() Function
- p5.js | hue() function
- p5.js | red() function
- PHP Ds\Set get() Function
- PHP | exp() Function
- D3.js | d3.hcl() Function
- p5.js | min() function
- p5.js | nfp() Function
- D3.js | d3.set.add() Function
- p5.js | nfs() Function
- PHP Ds\Set sum() Function
- PHP | dir() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.