Current Location: Home> Function Categories> array_fill

array_fill

Fill the array with the given value
Name:array_fill
Category:Array
Programming Language:php
One-line Description:Fill the array with the given key value.

array_fill function

Applicable to PHP version

PHP 4.0.0 and above

Function description

The array_fill function is used to fill an array. It will fill the specified range of an array with the given value, and both the starting index and the array size can be customized.

Function Syntax

array_fill(int $start_index, int $num, mixed $value): array

parameter

  • $start_index (int): The array start index (including this index).
  • $num (int): The number of array elements to be filled.
  • $value (mixed): The value used to fill the array. Can be of any type.

Return value

Returns a filled array, the starting index of the array is the specified value, the length is the specified number, and the value of each element is the provided fill value.

Example

  <?php
  // 创建一个从索引5开始,包含10个元素,且所有元素的值为“apple”的数组
  $result = array_fill(5, 10, "apple");
<p>// Output result<br>
print_r($result);<br>
?><br>

The above code creates an array starting from index 5 and fills with "apple" 10 elements starting from index 5.

Description of sample code

In the above example, `array_fill(5, 10, "apple")` means that starting from the index 5 of the array, filling 10 elements, all elements have values "apple". The final output array will start from index 5 and contain 10 "apple" elements:

  Array
  (
      [5] => apple
      [6] => apple
      [7] => apple
      [8] => apple
      [9] => apple
      [10] => apple
      [11] => apple
      [12] => apple
      [13] => apple
      [14] => apple
  )
  
Similar Functions
Popular Articles