PHP | array_fill()函数

array_fill()是PHP中的一个内置函数,用于用值填充数组。此函数基本上创建一个具有给定预填充值的用户定义数组。

null

语法:

array_fill($start_index, $number_elements, $values)

参数: array_fill()函数有三个参数,如下所述:

  1. $start_索引: 此参数指定将值填充到用户想要创建的数组中的起始位置。如果$start_index为负,则返回数组的第一个索引将为$start_index,以下索引将从零开始。所以最好给它指定一个正值。这是一个强制性参数,必须提供。
  2. $number_元素: 此参数指的是用户想要进入数组的元素数。$number_元素应该是正的(包括0,对于5.6.0版),否则会抛出E_警告。这也是一个强制参数。
  3. 美元价值: 此参数指的是要插入数组的值。这些值可以是任何类型。

返回类型 :array_fill()函数返回一个填充的用户定义数组,其值由 美元价值 参数

例如:

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
        )

下面的程序说明了数组_fill()函数在PHP中的工作原理:

<?php
// PHP code to illustrate the working of array_fill()
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 ));
?>


输出:

Array
(
    [2] => Geeks
    [3] => Geeks
    [4] => Geeks
    [5] => Geeks
    [6] => Geeks
)

参考 : http://php.net/manual/en/function.array-fill.php

© 版权声明
THE END
喜欢就支持一下吧
点赞11 分享