php 如何声明一个二维数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1811100/
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 do I declare a two dimensional array?
提问by Mask
What's the easiest way to create a 2d array. I was hoping to be able to do something similar to this:
创建二维数组的最简单方法是什么。我希望能够做类似的事情:
declare int d[0..m, 0..n]
回答by Atli
You can also create an associative array, or a "hash-table" like array, by specifying the index of the array.
您还可以通过指定数组的索引来创建关联数组或类似“哈希表”的数组。
$array = array(
0 => array(
'name' => 'John Doe',
'email' => '[email protected]'
),
1 => array(
'name' => 'Jane Doe',
'email' => '[email protected]'
),
);
Which is equivalent to
这相当于
$array = array();
$array[0] = array();
$array[0]['name'] = 'John Doe';
$array[0]['email'] = '[email protected]';
$array[1] = array();
$array[1]['name'] = 'Jane Doe';
$array[1]['email'] = '[email protected]';
回答by David Snabel-Caunt
The following are equivalent and result in a two dimensional array:
以下是等效的,并产生一个二维数组:
$array = array(
array(0, 1, 2),
array(3, 4, 5),
);
or
或者
$array = array();
$array[] = array(0, 1, 2);
$array[] = array(3, 4, 5);
回答by Kornel
Just declare? You don't have to. Just make sure variable exists:
就声明一下?你不必。只要确保变量存在:
$d = array();
Arrays are resized dynamically, and attempt to write anything to non-exsistant element creates it (and creates entire array if needed)
数组会动态调整大小,并尝试向不存在的元素写入任何内容以创建它(并在需要时创建整个数组)
$d[1][2] = 3;
This is valid for any number of dimensions without prior declarations.
这对于没有事先声明的任意数量的维度都是有效的。
回答by cletus
Firstly, PHP doesn't have multi-dimensional arrays, it has arrays of arrays.
首先,PHP 没有多维数组,它有数组的数组。
Secondly, you can write a function that will do it:
其次,您可以编写一个函数来执行此操作:
function declare($m, $n, $value = 0) {
return array_fill(0, $m, array_fill(0, $n, $value));
}
回答by Kingsolmn
For a simple, "fill as you go" kind of solution:
对于一个简单的“随用随填”的解决方案:
$foo = array(array());
$foo = array(array());
This will get you a flexible pseudo two dimensional array that can hold $foo[n][n] where n<= ∞ (of course your limited by the usual constraints of memory size, but you get the idea I hope). This could, in theory, be extended to create as many sub arrays as you need.
这将为您提供一个灵活的伪二维数组,它可以保存 $foo[ n][ n] 其中n<= ∞ (当然,您受到内存大小的通常限制,但我希望您明白这个想法)。理论上,这可以扩展为根据需要创建尽可能多的子数组。
回答by Amber
Or for larger arrays, all with the same value:
或者对于更大的数组,都具有相同的值:
$m_by_n_array = array_fill(0, $n, array_fill(0, $m, $value);
will create an $mby $narray with everything set to $value.
将创建一个$mby$n数组,所有内容都设置为$value.
回答by Ray
As far as I'm aware there is no built in php function to do this, you need to do it via a loop or via a custom method that recursively calls to something like array_fill inidcated in the answer by @Amber;
据我所知,没有内置的 php 函数可以执行此操作,您需要通过循环或自定义方法来执行此操作,该方法递归调用@Amber 在答案中指定的类似 array_fill 的内容;
I'm assuming you mean created an empty but intialized array of arrays. For example, you want a final results like the below of a array of 3 arrays:
我假设您的意思是创建了一个空的但已初始化的数组数组。例如,您想要一个包含 3 个数组的数组的最终结果,如下所示:
$final_array = array(array(), array(), array());
This is simple to just hand code, but for an arbitrary sized array like a an array of 3 arrays of 3 arraysit starts getting complex to initialize prior to use:
这对于手动代码来说很简单,但是对于任意大小的数组,例如3 个数组的 3 个数组的数组,在使用之前初始化变得复杂:
$final_array = array(array(array(), array(), array()), array(array(), array(), array()), array(array(), array(), array()));
...etc...
...等等...
I get the frustration. It would be nice to have an easy way to declare an initialized array of arrays any depth to use without checking or throwing errors.
我明白了。有一种简单的方法来声明一个已初始化的数组数组,无需检查或抛出错误即可使用任何深度,这将是很好的。
回答by user824232
And for me the argument about whether an array should be sparse or not depends on the context.
对我来说,关于数组是否应该稀疏的争论取决于上下文。
For example, if $a[6][9] is not populated is the equivalent to $a[6][9] being populated with for example with "" or 0.
例如,如果 $a[6][9] 未填充相当于 $a[6][9] 填充了例如“”或 0。
回答by Deshal Kh
$r = array("arr1","arr2");
to echo a single array element you should write:
要回显您应该编写的单个数组元素:
echo $r[0];
echo $r[1];
output would be: arr1 arr2
输出将是: arr1 arr2
回答by Oskar
And I like this way:
我喜欢这种方式:
$cars = array
(
array("Volvo",22),
array("BMW",15),
array("Saab",5),
array("Land Rover",17)
);

