超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<array_diffarray_filter>
Last updated: Fri, 22 Jun 2007

array_fill

(PHP 4 >= 4.2.0, PHP 5)

array_fill -- 用给定的值填充数组

说明

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

array_fill()value 参数的值将一个数组填充 num 个条目,键名由 start_index 参数指定的开始。注意 num 必须是一个大于零的数值,否则 PHP 会发出一条警告。

例子 1. array_fill() 例子

<?php
$a
= array_fill(5, 6, 'banana');
print_r($a);
?>

$a 现在是:

Array
(
    [5]  => banana
    [6]  => banana
    [7]  => banana
    [8]  => banana
    [9]  => banana
    [10] => banana
)

参见 str_repeat()range()




add a note add a note User Contributed Notes
array_fill
26-Aug-2002 04: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.

<array_diffarray_filter>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22