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



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

Filters the collection using the given callback

Example:

<?php
function tips_reject($items, $func)
{
  return array_values(array_diff($items, array_filter($items, $func)));
}

print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) {
  return strlen($item) > 4;
}));
?>

Output:

Array
(
    [0] => Pear
    [1] => Kiwi
)