在 PHP 中创建一个空的二维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6613307/
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
Creating an empty 2D array in PHP?
提问by Joshua
I know that arrays are created dynamically, and creating them ahead of time isn't really necessary, but how would one do that with a 2D array? The same way?
我知道数组是动态创建的,提前创建它们并不是真正必要的,但是如何使用二维数组来做到这一点?以同样的方式?
(for$j)
{
for($i)
{
$array[j][i] = "data";
}
}
Something like that? Obviously real for loops, of course.
类似的东西?当然,显然是真正的 for 循环。
回答by Brendan Bullen
At its absolute simplest, a 2D dimensional array can be created as:
在最简单的情况下,可以将二维数组创建为:
<?php
$emptyArray = array(array());
?>
Or as of PHP 5.4 you can also use:
或者从 PHP 5.4 开始,您还可以使用:
<?php
$emptyArray = [[]];
?>
回答by Christian
You don't create a 2d array in PHP, not in the traditional sense.
您不会在 PHP 中创建二维数组,而不是传统意义上的。
The suggestions above about $a = array(array());
in fact simply creating the following array:
上面关于$a = array(array());
实际上只是创建以下数组的建议:
$a = array(
0 => array( )
);
Therefore, $a[0][0] = 'test';
would result in the following:
因此,$a[0][0] = 'test';
会导致以下结果:
$a = array(
0 => array(
0 => 'test'
)
);
At a first glance, it looks like it works, but in fact, this is still not a 2d array. When you try to use the 2nd row (index 1), at this point, PHP would throw a notice. Eg:
乍一看,它看起来很有效,但实际上,这仍然不是一个二维数组。当您尝试使用第 2 行(索引 1)时,此时 PHP 会抛出一个通知。例如:
$a[1][0] = 'test2';
This is because $a[1]
does not contain anything (remember that array(array())
simply creating an array at index 0?).
这是因为$a[1]
不包含任何内容(还记得array(array())
简单地在索引 0 处创建一个数组吗?)。
For it to work, you need to yet again do $a[1] = array();
, or if you want to avoid indices you can do, $a[] = array();
.
为了让它工作,你需要再次做$a[1] = array();
,或者如果你想避免索引,你可以做,$a[] = array();
.
Example
例子
$a = array(); // array of columns
for($c=0; $c<5; $c++){
$a[$c] = array(); // array of cells for column $c
for($r=0; $r<3; $r++){
$a[$c][$r] = rand();
}
}
The above code creates a 5x3 "2d array" of random numbers.
上面的代码创建了一个 5x3 的随机数“二维数组”。
回答by Henrik P. Hessel
The PHP documentationis always a good way to start for these kind of basic questions.
该PHP文件是总是开始为这几样基本问题的好办法。
<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));
echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
回答by Christian Smorra
Could you specify what you are trying to do? You can loop through multidimensional arrays with the foreach function
你能具体说明你想要做什么吗?您可以使用 foreach 函数遍历多维数组
$ary=array( "subarr" => array("foo","bar") );
foreach($ary as $a){
foreach($a as $ary_sub){
echo $ary_sub;
}
}
or
或者
foreach($ary["subarr"] as $key=>$subval){
echo $subval;
}
回答by Adrian Kentleton
If I want to create an empty array for handling lines from text files, I just use $array = array();
如果我想创建一个空数组来处理文本文件中的行,我只需使用 $array = array();
回答by Ivanda Nothabeer
Using "array(array())" will create a 2D array with an "empty" element in the first position. To create a truly blank 2D array this needs to be removed.
使用“array(array())”将创建一个二维数组,在第一个位置有一个“空”元素。要创建真正空白的 2D 阵列,需要将其删除。
<?php
$emptyArray = array(array()); // Creates a 2D array with one empty element in $emptyArray[0]
array_pop($emptyArray); // Pops element[0] off the array
?>
回答by Samcoder
// dynamic 2D array
$twoD = array(array());
$val = 0;
// fill the array
for($r = 0; $r < 4; $r++) {
for($c = 0; $c < 4; $c++)
$twoD[$r][$c] = $val++;
}
// print the current value of $val
echo $val."<br/>------------------<br/>";
// print the array
for($r = 0; $r < 4; $r++) {
for($c = 0; $c < 4; $c++)
echo $twoD[$r][$c];
echo "<br/>";
}
回答by Teocci
Remember that whenever you use array(array())
nested function or the short array syntax [[]]
both will create a 2D array with an empty
element in the first position. This may bring you some errors so we needs to be removed it.
请记住,无论何时使用array(array())
嵌套函数或短数组语法,[[]]
都将创建一个empty
元素位于第一个位置的二维数组。这可能会给您带来一些错误,因此我们需要将其删除。
How we can remove an empty
element?
我们如何删除一个empty
元素?
Well it is absolute simple as calling the array_pop()
method to pop the element[0]
off the array, as this:
那么调用array_pop()
方法来弹出element[0]
数组绝对简单,如下所示:
<?php
$a = array(array());
echo 'Before removing the empty element: \n'
print_r($a);
array_pop($a);
print_r($a);
$b = [[]];
echo 'Before removing the empty element: \n'
print_r($b);
array_pop($b);
print_r($b);
?>