如何在 PHP 中创建一个预定义大小的空数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5385433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an empty array in PHP with predefined size?
提问by Reed Richards
I am creating a new array in a for loop.
我正在 for 循环中创建一个新数组。
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.
PHP 一直抱怨偏移量,因为每次迭代我都会为数组添加一个新索引,这有点愚蠢。
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 /..
Is there some way to predefine the number items in the array so that PHP will not show this notice?
有没有办法预先定义数组中的数字项,这样PHP就不会显示这个通知?
In other words, can I predefine the size of the array in a similar way to this?
换句话说,我可以以与此类似的方式预定义数组的大小吗?
$myarray = array($size_of_the_earray);
回答by Jon
There is no way to create an array of a predefined size without also supplying values for the elements of that array.
如果不为该数组的元素提供值,就无法创建一个预定义大小的数组。
The best way to initialize an array like that is array_fill
. By far preferable over the various loop-and-insert solutions.
像这样初始化数组的最佳方法是array_fill
. 远远优于各种循环和插入解决方案。
$my_array = array_fill(0, $size_of_the_array, $some_data);
Every position in the $my_array
will contain $some_data
.
中的每个位置都$my_array
将包含$some_data
.
The first zero in array_fill
just indicates the index from where the array needs to be filled with the value.
中的第一个零array_fill
仅表示需要用值填充数组的索引。
回答by Jean-Philippe Leclerc
You can't predefine a size of an array in php. A good way to acheive your goal is the following:
您不能在 php 中预定义数组的大小。实现目标的一个好方法是:
// Create a new array.
$array = array();
// Add an item while $i < yourWantedItemQuantity
for ($i = 0; $i < $number_of_items; $i++)
{
array_push($array, $some_data);
//or $array[] = $some_data; for single items.
}
Note that it is way faster to use array_fill() to fill an Array :
请注意,使用 array_fill() 填充 Array 会更快:
$array = array_fill(0,$number_of_items, $some_data);
If you want to verify if a value has been set at an index, you should use the following: array_key_exists("key", $array) or isset($array["key"])
如果要验证是否已在索引处设置值,应使用以下命令:array_key_exists("key", $array) 或 isset($array["key"])
See array_key_exists, issetand array_fill
回答by Madbreaks
Possibly related, if you want to initialize and fill an array with a range of values, use PHP's (wait for it...) range function:
可能相关,如果你想用一系列值初始化和填充数组,请使用 PHP 的(等待它......)范围函数:
$a = range(1, 5); // array(1,2,3,4,5)
$a = range(0, 10, 2); // array(0,2,4,6,8,10)
回答by Jacob
PHP Arraysdon't need to be declared with a size.
PHP 数组不需要声明大小。
An array in PHP is actually an ordered map
PHP 中的数组实际上是一个有序映射
You also shouldn't get a warning/notice using code like the example you have shown. The common Notice people get is "Undefined offset" when reading from an array.
您也不应该使用像您显示的示例那样的代码收到警告/通知。从数组中读取时,人们得到的常见通知是“未定义的偏移量”。
A way to counter this is to check with isset
or array_key_exists
, or to use a function such as:
解决此问题的一种方法是使用isset
或检查array_key_exists
,或使用以下函数:
function isset_or($array, $key, $default = NULL) {
return isset($array[$key]) ? $array[$key] : $default;
}
So that you can avoid the repeated code.
这样就可以避免重复的代码。
Note: isset
returns false if the element in the array is NULL, but has a performance gain over array_key_exists
.
注意:isset
如果数组中的元素为 NULL,则返回 false,但性能提升超过array_key_exists
.
If you want to specify an array with a size for performance reasons, look at:
如果出于性能原因要指定具有大小的数组,请查看:
SplFixedArrayin the Standard PHP Library.
标准 PHP 库中的SplFixedArray。
回答by Academia
回答by Chandan Sharma
PHP provides two types of array.
PHP 提供了两种类型的数组。
- normal array
- SplFixedArray
- 普通数组
- 固定数组
normal array : This array is dynamic.
普通数组:这个数组是动态的。
SplFixedArray : this is a standard php library which provides the ability to create array of fix size.
SplFixedArray :这是一个标准的 php 库,它提供了创建固定大小数组的能力。
回答by user10620381
$array = new SplFixedArray(5);
echo $array->getSize()."\n";
You can use PHP documentation more info check this link https://www.php.net/manual/en/splfixedarray.setsize.php
您可以使用 PHP 文档了解更多信息,请查看此链接 https://www.php.net/manual/en/splfixedarray.setsize.php