PHP array fill

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP: array_fill() function

PHP: Fill an array with values

The array_fill() function is used to fill an array with values.

The function fills an array with num entries of the value of the array_values parameter, keys starting at the starting_index parameter.

Version

(PHP 4 and above)

Syntax

array_fill(starting_index, num_of_elements, array_values )

Parameter

Name

Description

Required /
Optional

Type

starting_index

The first index of the returned array. Allows non-negative indexes only.

Required

Integer

num_of_elements

Number of elements to insert.

Required

Integer

array_values

Values to be used for filling.

Required

Mixed*

*Mixed : Mixed indicates that a parameter may accept multiple (but not necessarily all) types.

Return value

The filled array.

Value Type : Array

Pictorial Presentation :

php-array-array_fill()

Example :

  1. $val = array_fill(10,5, 'Geography');  

  2. print_r($val);  

  3. ?>  

Output :

Array ( [10] => Geography [11] => Geography [12] => Geography [13] => Geography [14] => Geography )