473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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:

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.         }

2 4667
deric
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
Great. Thanks a lot!
May 7 '12 #3

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

Similar topics

4
by: Supra | last post by:
i saved file in binary. i saved 16 colours.. but how do i read back values into listbox? Dim st2 As Stream = File.Open _ ("C:\Documents and Settings\My Documents\Visual Studio...
40
by: Abby | last post by:
My .dat file will contain information like below. /////////// First 0x04 0x05 0x06 Second 0x07
19
by: Holger Hasselbach | last post by:
- The value of the object allocated by the malloc function is used (7.20.3.3). - The value of any bytes in a new object allocated by the realloc function beyond the size of the old object are used...
2
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
4
by: kj | last post by:
I consider myself quite proficient in C and a few other programming languages, but I have never succeeded in understanding a largish program (such as zsh or ncurses) at the source level. ...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
5
by: ashley.ward | last post by:
I am attempting to write a program with VB 2005 Express Edition which accesses an Oracle 9 database and dumps the results of three SELECT queries into a spreadsheet file once every hour. ...
5
by: Denis Petronenko | last post by:
Hello, how can i read into strings from ifstream? file contains values in following format: value11; val ue12; value 13; valu e21;value22; value23; etc. i need to read like file >string,...
0
by: happy | last post by:
you can take a look for this web sites http://www.imanway.com/vb/forumdisplay.php?f=90 http://www.geocities.com/islamone_l/index.htm or read this ...
6
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, can someone please let me know how I can read xml elements using object oriented program. I created a class to use the get and set properties however I dont know how I can pass the values from...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.