How to read from a three-dimensional array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • luftikus143
    New Member
    • Jan 2007
    • 97

    How to read from a three-dimensional array

    Hi there,

    gush, this array issue doesn't really want to enter my head. Always having problem with it.

    I have, after quite some effort, succeeded in storing data in an array, which looks like this:

    Code:
    array(10) {
      ["Economically Active Population"]=>
      array(41) {
        [0]=>
        array(2) {
          [0]=>
          string(4) "1980"
          [1]=>
          string(3) "179"
        }
        [1]=>
        array(2) {
          [0]=>
          string(4) "1981"
          [1]=>
          string(3) "179"
        }
        [2]=>
        array(2) {
          [0]=>
          string(4) "1982"
          [1]=>
          string(3) "179"
        }
    	......
      }
      ["Population - Aged 0 - 14"]=>
      array(62) {
        [0]=>
        array(2) {
          [0]=>
          string(4) "1980"
          [1]=>
          string(3) "179"
        }
        [1]=>
        array(2) {
          [0]=>
          string(4) "1981"
          [1]=>
          string(3) "179"
        }
        [2]=>
        array(2) {
          [0]=>
          string(4) "1982"
          [1]=>
          string(3) "179"
        }
        .......

    Now, I need to read from that array. And there, I don't succeed in getting it correctly off the ground.

    I want to display the variable name in a cell, and then (at least for the moment) only the values (not the years) in following ones, and then turn to the next row.

    This is how the code looks like, but the result is somewhat strange, putting me multiple rows with the same rows, in which all items appear, one after the other:

    Code:
        for ($z = 0; $z < 10; $z++)
        {
    		echo "<tr>";
    		foreach ($arr as $key1 => $item)
    		{ 
    			echo "<td>" . $key1 . "</td>";
    			foreach($item as $key => $value)
    			{
    				echo "<td>" . $value[1] . "</td>";
    			}
    		}
    		echo "</tr>";
    	}

    Can anyone give me a hint what the proper way of writing is, to retrieve the elements correctly?

    Thanks a lot.
  • deric
    New Member
    • Dec 2007
    • 92

    #2
    Remove the first loop because it's useless, and then transfer the opening and closing <tr> inside the second loop.

    Code:
    foreach ($arr as $key1 => $item)
            { 
                echo "<tr>";
                echo "<td>" . $key1 . "</td>";
                foreach($item as $key => $value)
                {
                    echo "<td>" . $value[1] . "</td>";
                }
                echo "</tr>";
            }

    Comment

    • luftikus143
      New Member
      • Jan 2007
      • 97

      #3
      Great. Thanks a lot!

      Comment

      Working...