简体   繁体   中英

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 TestEl: 1 | TestEl: 2 | TestEl: 3 TestEl: 1 | TestEl: 2 | TestEl: 3 , as expected.

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 .

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 ?

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. Instead you want $nodeList[$i] to be an array. PHP does that implicitely (creating the array) when you access it with brackets [...] , so you can just leave out the offending statement, and do:

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:

for($i = 0; $i < 10;$i++) {
    for($j = 0; $j < 3;$j++) {
        $nodeList[$i][] = $j;
    }
}

Adding a value at every level

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:

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:

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=array();

and for 2D array

$nodeList= array(array());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM