如何在PHP中创建一个空数组predefined大小? [英] How to create an empty array in PHP with predefined size?

查看:174
本文介绍了如何在PHP中创建一个空数组predefined大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在创建一个新的数组循环。

 为$ I< $ NUMBER_OF_ITEMS
    $数据[$ i] = $ some_data;

PHP一直在抱怨,因为偏移每个迭代我添加了一个新的指数为阵,这是一种愚蠢的。

注意:未定义抵消:1的包括()的(/ 23行...
注意:未定义抵消:在包括1()(23行/ ..的
注意:未定义抵消:在包括1()/..

我该如何解决这个问题?是否有某种方式为predefine数组中的一些项目,使PHP不会抛出愚蠢的错误?像

  $ myArray的=阵列($ size_of_the_earray);


解决方案

有既不是一个办法,也没有必要这么做。

为后人

初​​始化这样一个数组是 array_fill 。到目前为止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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆