PHP array_fill() Function
Last Updated : 17 Mar 2025
The array_fill( ) function is used to fill an array with values. This function creates a user-defined array with a given pre-filled value. It was introduced in PHP 4.
Syntax
Parameter
| Parameter | Description | Is compulsory |
|---|---|---|
| starting_index | The first index of the returned array. Allows non-negative index only. | compulsory |
| num_of_elements | Number of elements to insert. | compulsory |
| array_values | Values to be used for filling. | compulsory |
Return Type
It returns the filled array.
EXAMPLE 1
Output:
Array ( [10] => javatpoint [11] => javatpoint [12] => javatpoint [13] => javatpoint [14] => javatpoint [15] => javatpoint )
EXAMPLE 2
Output:
Array ( [5] => Mango [6] => Mango [7] => Mango [8] => Mango [9] => Mango [10] => Mango )
EXAMPLE 3
Output:
Array ( [5] => Apple [6] => Apple [7] => Apple [8] => Apple [9] => Apple ) Array ( [-2] => Banana [0] => Banana [1] => Banana )
EXAMPLE 4
Output:
Array ( [0] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [1] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
EXAMPLE 5
Output:
Array ( [0] => [1] => [2] => [3] => [4] => ) Array ( [0] => 1 [1] => 1 [2] => 1 [3] => 1 [4] => 1 )
Next TopicPhp-array-fill-keys-function