如何在PHP中创建一个空数组predefined大小? [英] How to create an empty array in PHP with predefined size?
问题描述
我在创建一个新的数组循环。
为$ I< $ NUMBER_OF_ITEMS
$数据[$ i] = $ some_data;
PHP一直在抱怨,因为偏移每个迭代我添加了一个新的指数为阵,这是一种愚蠢的。
注意:未定义抵消:1的包括()的(/ 23行...
注意:未定义抵消:在包括1()(23行/ ..的
注意:未定义抵消:在包括1()/..的P(23行>
我该如何解决这个问题?是否有某种方式为predefine数组中的一些项目,使PHP不会抛出愚蠢的错误?像
$ myArray的=阵列($ size_of_the_earray);
有既不是一个办法,也没有必要这么做。
为后人
初始化这样一个数组是 array_fill $ C的最佳方式$ C>
。到目前为止preferable在各环路和插入的解决方案。
I am creating a new array in a for loop.
for $i < $number_of_items
$data[$i] = $some_data;
PHP keeps complaining about the offset since for each iteration I add a new index for the array, which is kind of stupid.
Notice: Undefined offset: 1 in include() (line 23 of /... Notice: Undefined offset: 1 in include() (line 23 of /.. Notice: Undefined offset: 1 in include() (line 23 of /..
How do I solve this? Is there some way to predefine the number items in the array so that php wont throw the stupid error? Like
$myarray = array($size_of_the_earray);
There is neither a way nor a need to do that.
For posterity
The best way to initialize an array like that is array_fill
. By far preferable over the various loop-and-insert solutions.
这篇关于如何在PHP中创建一个空数组predefined大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!