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.
Syntax:
array_fill($start_index, $number_elements, $values)
Parameter:
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.
Examples:
Input : $start_index = 2; $number_elements = 3;
$values = "Geeks";
Output :
Array
(
[2] => Geeks
[3] => Geeks
[4] => Geeks
)
Input : $start_index = -10; $number_elements = 3;
$values = 45;
Output :
Array
(
[-10] => 45
[0] => 45
[1] => 45
)
Below program illustrates the working of array_fill() function in PHP:
<?php // PHP code to illustrate the working of array_fill() function Fill($start_index, $number_elements, $values){ return(array_fill($start_index, $number_elements, $values)); } // Driver Code $start_index = 2; $number_elements = 5; $values = "Geeks"; print_r(Fill($start_index, $number_elements, $values)); ?> |
Output:
Array
(
[2] => Geeks
[3] => Geeks
[4] => Geeks
[5] => Geeks
[6] => Geeks
)
Reference: http://php.net/manual/en/function.array-fill.php
Recommended Posts:
- How to Check a Function is a Generator Function or not using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.map.has() Function
- p5.js | min() function
- PHP | dir() Function
- p5.js | second() function
- p5.js | max() function
- D3.js | d3.map.get() Function
- D3.js | d3.hsl() Function
- p5.js | hue() function
- CSS min() Function
- p5.js | cos() function
- p5.js | sin() function
- p5.js | log() function
- p5.js | red() function
- p5.js | arc() Function
- D3.js | d3.map.set() Function
- p5.js | int() 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 contribute@geeksforgeeks.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.

