w3resource

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

Example:

<?php
$val = array_fill(10,5, 'Geography');
print_r($val);
?>

Output:

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

Pictorial Presentation:

php function reference: array_fill() function

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous:array_diff
Next: array_filter



PHP: Tips of the Day

PHP: Extract numbers from a string

$str = 'In My Cart : 11 12 items';
preg_match_all('!\d+!', $str, $matches);
print_r($matches);