+

PHP array_fill () function

Syntax:
array_fill ($start_index, $number_elements, $values)
Parameter:
The array_fill() function takes three parameters and is described below:
  • $start_index:This parameter specifies the starting position of filling the values ​​in the array that the user wants to create. If $start_index is negative, the first index of the returned array will be $start_index, and subsequent indices will start at zero. So it`s better to give it a positive value. This parameter is required and must be specified.
  • $number_elements:This parameter refers to the number of elements that the user wants to enter into the array. $Number_elements must be positive (including 0, for version 5.6.0), otherwise E_WARNING is thrown. This is also a required parameter.
  • $values:this parameter refers to the values ​​that we want to insert into the array. These values ​​can be of any type.
  • Return type : array_fill() returns a filled custom array, the values ​​of which are described by the $value parameter .Examples:
    Input: $start_index = 2; $number_elements = 3; $values ​​= "Geeks"; Output: Array ([2] = > Geeks [3] = > Geeks [4] = > Geeks) Input: $start_index = -10; $number_elements = 3; $values ​​= 45; Output: Array ([-10] = > 45 [0] = > 45 [1] = > 45)
    The following program illustrates how the array_fill() function in PHP works:  
    // PHP code to illustrate how array_fill() works function Fill ( $start_index , $number_elements , $values ​​ ) { return ( array_fill ( $start_index , $number_elements , $values ​​ )); }  
    // Driver code $start_index = 2; $number_elements = 5; $values ​​ = "Geeks" ; print_r (Fill ( $start_index , $number_elements , $values ​​ )); ?>
    Output:
    Array ([ 2] = > Geeks [3] = > Geeks [4] = > Geeks [5] = > Geeks [6] = > Geeks)
    Link: http://php.net/manual/en/function.array-fill.php
    Get Solution for free from DataCamp guru