array_fill

(PHP 4 >= 4.2.0, PHP 5, PHP 7, PHP 8)

array_fill用给定的值填充数组

说明

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

value 参数的值填充 count 个元素到数组, 开始键名由 start_index 参数指定。

参数

start_index

返回的数组的第一个索引值。

如果 start_index 是负数, 那么返回的数组的第一个索引将会是 start_index ,而后面索引则从0开始。 (参见 例子)。

count

要插入元素的数量。 必须大于或等于 0。

value

用来填充的值。

返回值

返回填充后的数组。

错误/异常

如果 count 小于零,将会抛出 E_WARNING

更新日志

版本 说明
5.6.0 count 现在可以是零。 之前 count 必须大于零。

范例

Example #1 array_fill() 例子

<?php
$a 
array_fill(56'banana');
$b array_fill(-24'pear');
print_r($a);
print_r($b);
?>

以上例程会输出:

Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)
Array
(
    [-2] => pear
    [0] => pear
    [1] => pear
    [2] => pear
)

注释

参见手册上 数组 一节里关于负数的键的详细解释。

参见

User Contributed Notes

user at NOSPAM dot example dot com 30-Jun-2021 04:28
As of PHP 8.0 the example code

<?php
$b
= array_fill(-2, 4, 'pear');
print_r($b);
?>

now returns

Array
(
    [-2] => pear
    [-1] => pear
    [0] => pear
    [1] => pear
)

See https://wiki.php.net/rfc/negative_array_index and https://github.com/php/php-src/pull/3772
Hayley Watson 13-Jan-2018 11:57
Fill missing keys in a (numerically-indexed) array with a default value

<?php

function fill_missing_keys($array, $default = null, $atleast = 0)
{
    return
$array + array_fill(0, max($atleast, max(array_keys($array))), $default);
}

?>
anatoliy at ukhvanovy dot name 16-Jun-2014 10:00
If you need negative indices:
<?php
$b
= array_fill(-2, 4, 'pear');//this is not what we want
$c = array_fill_keys(range(-2,1),'pear');//these are negative indices
print_r($b);
print_r($c);
?>
Here is result of the code above:
Array
(
    [-2] => pear
    [0] => pear
    [1] => pear
    [2] => pear
)
Array
(
    [-2] => pear
    [-1] => pear
    [0] => pear
    [1] => pear
)
mchljnk at NOSPAM dot gmail dot com 10-Nov-2013 03:07
Using objects with array_fill may cause unexpected results. Consider the following:

<?php
class Foo {
   public
$bar = "banana";
}

//fill an array with objects
$array = array_fill(0, 2, new Foo());

var_dump($array);
/*
array(2) {
  [0]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(6) "banana"
  }
  [1]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(6) "banana"
  }
} */

//now we change the attribute of the object stored in index 0
//this actually changes the attribute for EACH object in the ENTIRE array
$array[0]->bar = "apple";

var_dump($array);
/*
array(2) {
  [0]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(5) "apple"
  }
  [1]=>
  object(Foo)#1 (1) {
    ["bar"]=>
    string(5) "apple"
  }
}
 */
?>

Objects are filled in the array BY REFERENCE. They are not copied for each element in the array.
miguelxpain at gmail dot com 24-Feb-2012 06:04
I made this function named "array_getMax" that returns te maximum value and index, from array:

<?php
//using array_search_all by helenadeus at gmail dot com

function array_search_all($needle, $haystack)
{
#array_search_match($needle, $haystack) returns all the keys of the values that match $needle in $haystack

   
foreach ($haystack as $k=>$v) {
  
        if(
$haystack[$k]==$needle){
      
          
$array[] = $k;
        }
    }
    return (
$array);

  
}

function
array_getMax($array){
       
   
$conteo=array_count_values($array);
   
    if(
count($conteo)==1 ){//returns full array when all values are the same.
       
return  $array;      
    }
      
   
arsort($array);
   
   
//$antValue=null;
   
$maxValue=null;
   
$keyValue=null;
    foreach(
$array as $key=>$value){
        if(
$maxValue==null){
           
$maxValue=$value;
           
$keyValue=$key;
            break;
        }       
    }
   
   
$resultSearch=array_search_all($maxValue, $array);
       
   return 
array_fill_keys($resultSearch, $maxValue);
   
   
}

//example
 
$arreglo=array('e1'=>99,'e2'=>'99','e3'=>1,'e4'=>1,'e5'=>98);
   
   
var_dump(array_getMax($arreglo));

//output
/*
array(2) {
  ["e1"]=>
  int(99)
  ["e2"]=>
  int(99)
}
*/
?>

I hope some one find this usefull
csst0266 at cs dot uoi dot gr 11-Aug-2004 10:32
This is what I recently did to quickly create a two dimensional array (10x10), initialized to 0:

<?php
  $a
= array_fill(0, 10, array_fill(0, 10, 0));
?>

This should work for as many dimensions as you want, each time passing to array_fill() (as the 3rd argument) another array_fill() function.
25-Aug-2002 01:59
array_fill() cannot be used to setup only missing keys in an array. This  may be necessary for example before using implode()  on a sparse filled array.
The solution is to use this function:

<?php
function array_setkeys(&$array, $fill = NULL) {
 
$indexmax = -1;
  for (
end($array); $key = key($array); prev($array)) {
    if (
$key > $indexmax)
     
$indexmax = $key;
  }
  for (
$i = 0; $i <= $indexmax; $i++) {
    if (!isset(
$array[$i]))
     
$array[$i] = $fill;
  }
 
ksort($array);
}
?>

This is usefull in some situations where you don't know which key index was filled and you want to preserve the association between a positioned field in an imploded array and the key index when exploding it.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有