[英]Creating, Accessing and understanding multidimensional arrays in php
I have implemented the following small example: 我实现了以下小示例:
$nodeList;
for($i = 0; $i < 10;$i++) {
$nodeList[$i] = $i;
for($j = 0; $j < 3;$j++) {
$nodeList[$i][$j] = $j;
}
}
foreach($nodeList[0] as $nodeEl) {
print "NodeEl: ".$nodeEl." | ";
}
print nl2br("\n\r");
$testList = array
(
array(1,2,3),
array(4,5,6),
array(7,8,9),
array(10,11,12),
);
foreach($testList[0] as $testEl) {
print "TestEl: ".$testEl." | ";
}
Where the output for $nodeList
is null
(var_dump / print_r too) and the output for $testList
is TestEl: 1 | TestEl: 2 | TestEl: 3
其中$nodeList
的输出为null
(也是var_dump / print_r), $testList
的输出为TestEl: 1 | TestEl: 2 | TestEl: 3
TestEl: 1 | TestEl: 2 | TestEl: 3
TestEl: 1 | TestEl: 2 | TestEl: 3
, as expected. TestEl: 1 | TestEl: 2 | TestEl: 3
,符合预期。
In my understanding those two solutions should create roughly the same output - but instead there is no output for the first one at all. 以我的理解,这两个解决方案应该创建大致相同的输出-但是,第一个解决方案根本没有输出。 Because the second dimension of the array is not even created. 因为甚至没有创建数组的第二维。
Reading up on http://php.net/manual/de/language.types.array.php creates the strong feeling that the [] operator is only for dereferencing / accessing of the array, but then again the docs provide a sample where they assign a value to a certain key the same way I do $arr["x"] = 42
. 在http://php.net/manual/de/language.types.array.php上阅读后,会产生强烈的感觉,即[]运算符仅用于解引用/访问数组,但是文档再次提供了示例,其中他们以与我执行$arr["x"] = 42
相同的方式为某个键分配值。
What is the difference between these two ways of array access? 这两种数组访问方式有什么区别?
How can I achieve filling a n-dimensional array in a way similar to the way I try to fill $nodeList
? 如何以类似于尝试填充$nodeList
的方式实现填充n维数组?
You should make sure to have error reporting turned on, because warnings are generated for your code: 您应该确保已打开错误报告,因为会为您的代码生成警告:
E_WARNING : type 2 -- Cannot use a scalar value as an array -- at line 7
This concerns the following statement: 这涉及以下语句:
$nodeList[$i] = $i;
If you want to create a 2D array, there is no meaning in assigning a number on the first level. 如果要创建2D数组,则在第一级分配数字没有任何意义。 Instead you want $nodeList[$i]
to be an array. 相反,您希望$nodeList[$i]
是一个数组。 PHP does that implicitely (creating the array) when you access it with brackets [...]
, so you can just leave out the offending statement, and do: 当您使用方括号[...]
访问PHP时,PHP会隐式地执行此操作(创建数组),因此您可以省去有问题的语句,然后执行以下操作:
for($i = 0; $i < 10;$i++) {
for($j = 0; $j < 3;$j++) {
$nodeList[$i][$j] = $j;
}
}
You can even leave out the $j
in the last bracket pair, which means PHP will just add to the array using the next available numerical index: 您甚至可以在最后一个括号对中省略$j
,这意味着PHP将使用下一个可用的数字索引将其添加到数组中:
for($i = 0; $i < 10;$i++) {
for($j = 0; $j < 3;$j++) {
$nodeList[$i][] = $j;
}
}
If you really need to store $i
at the first level of the 2D array, then consider using a more complex structure where each element is an associative array with two keys: one for the value and another for the nested array: 如果确实需要将$i
存储在2D数组的第一级,则考虑使用更复杂的结构,其中每个元素是具有两个键的关联数组:一个用于值,另一个用于嵌套数组:
for($i = 0; $i < 10; $i++) {
$nodeList[$i] = array(
"value" => $i,
"children" => array()
);
for($j = 0; $j < 3;$j++) {
$nodeList[$i]["children"][] = array(
"value" => "$i.$j" // just example of value, could be just $j
);
}
}
$nodeList
will be like this then: $nodeList
将会像这样:
array (
array (
'value' => 0,
'children' => array (
array ('value' => '0.0'),
array ('value' => '0.1'),
array ('value' => '0.2'),
),
),
array (
'value' => 1,
'children' => array (
array ('value' => '1.0'),
array ('value' => '1.1'),
array ('value' => '1.2'),
),
),
//...etc
);
You should write 你应该写
<?php
$nodeList;
for($i = 0; $i < 10;$i++) {
for($j = 0; $j < 3;$j++) {
$nodeList[$i][$j] = $j;
}
}
foreach($nodeList[0] as $nodeEl) {
print "NodeEl: ".$nodeEl." | ";
}
You need to declare $nodeList
as array like 您需要将$nodeList
声明为数组,例如
$nodeList=array();
and for 2D array 对于二维数组
$nodeList= array(array());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.