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:
View the example in the browser
Practice here online :
See also
Previous:array_diff
Next: array_filter
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/php/function-reference/array_fill.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics