By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,723 Members | 1,876 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,723 IT Pros & Developers. It's quick & easy.

How to read from a three-dimensional array

P: 97
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:

Expand|Select|Wrap|Line Numbers
  1. array(10) {
  2.   ["Economically Active Population"]=>
  3.   array(41) {
  4.     [0]=>
  5.     array(2) {
  6.       [0]=>
  7.       string(4) "1980"
  8.       [1]=>
  9.       string(3) "179"
  10.     }
  11.     [1]=>
  12.     array(2) {
  13.       [0]=>
  14.       string(4) "1981"
  15.       [1]=>
  16.       string(3) "179"
  17.     }
  18.     [2]=>
  19.     array(2) {
  20.       [0]=>
  21.       string(4) "1982"
  22.       [1]=>
  23.       string(3) "179"
  24.     }
  25.     ......
  26.   }
  27.   ["Population - Aged 0 - 14"]=>
  28.   array(62) {
  29.     [0]=>
  30.     array(2) {
  31.       [0]=>
  32.       string(4) "1980"
  33.       [1]=>
  34.       string(3) "179"
  35.     }
  36.     [1]=>
  37.     array(2) {
  38.       [0]=>
  39.       string(4) "1981"
  40.       [1]=>
  41.       string(3) "179"
  42.     }
  43.     [2]=>
  44.     array(2) {
  45.       [0]=>
  46.       string(4) "1982"
  47.       [1]=>
  48.       string(3) "179"
  49.     }
  50.     .......
  51.  

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:

Expand|Select|Wrap|Line Numbers
  1.     for ($z = 0; $z < 10; $z++)
  2.     {
  3.         echo "<tr>";
  4.         foreach ($arr as $key1 => $item)
  5.         { 
  6.             echo "<td>" . $key1 . "</td>";
  7.             foreach($item as $key => $value)
  8.             {
  9.                 echo "<td>" . $value[1] . "</td>";
  10.             }
  11.         }
  12.         echo "</tr>";
  13.     }
  14.  

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

Thanks a lot.
May 3 '12 #1

✓ answered by deric

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

Expand|Select|Wrap|Line Numbers
  1. foreach ($arr as $key1 => $item)
  2.         { 
  3.             echo "<tr>";
  4.             echo "<td>" . $key1 . "</td>";
  5.             foreach($item as $key => $value)
  6.             {
  7.                 echo "<td>" . $value[1] . "</td>";
  8.             }
  9.             echo "</tr>";
  10.         }

Share this Question
Share on Google+
2 Replies


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

Expand|Select|Wrap|Line Numbers
  1. foreach ($arr as $key1 => $item)
  2.         { 
  3.             echo "<tr>";
  4.             echo "<td>" . $key1 . "</td>";
  5.             foreach($item as $key => $value)
  6.             {
  7.                 echo "<td>" . $value[1] . "</td>";
  8.             }
  9.             echo "</tr>";
  10.         }
May 4 '12 #2

P: 97
Great. Thanks a lot!
May 7 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.