Home >>PHP Array Functions >PHP array_fill() Function

PHP array_fill() Function

PHP array_fill() Function

PHP array_fill() Function is used to fill any given array with some values. It creates an user-defined array with a given pre-filled value. It accepts three parameters $index, $number, and$value. It returns a filled user-defined array as output with values described by $value parameter.

Syntax:

array_fill($index, $number, $value);

Parameter Values

Parameter Description
index This is a required parameter. This parameter definesthe first index of the returned array.
number This is a required parameter. This parameter defines the number of elements to insert.
value This is a required parameter. This parameter definesthe value to use for filling the array.

Here is an example of array_fill() function in PHP:

<html>
<body>
<pre>
<?php
$x=array_fill(2,7,"Jerry");
print_r($x);
?>
</pre>
</body>
</html>
Output:
Array
(
    [2] => Jerry
    [3] => Jerry
    [4] => Jerry
    [5] => Jerry
    [6] => Jerry
    [7] => Jerry
    [8] => Jerry
)

Example 2:

<html>
<body>
<pre>
<?php
$x=array_fill(8,10,"Jerry");
print_r($x);
?>
</pre>
</body>
</html>
Output:
Array
(
    [8] => Jerry
    [9] => Jerry
    [10] => Jerry
    [11] => Jerry
    [12] => Jerry
    [13] => Jerry
    [14] => Jerry
    [15] => Jerry
    [16] => Jerry
    [17] => Jerry
)

No Sidebar ads