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

Multi dimensional array help please.

100+
P: 348
I have this multidimensional array that I was given but do not understand it. What I was attempting to do with these arrays was to build them and then replace the values inside the cells.

Here is the code for that
[PHP]$pages = array(
array(
"Title" => "First page",
"Tables" => array(
array(
"Title" => "First table",
"Rows" => array(
array(
"Cells" => array("Col1", "Col2", "Col3")
),
)
),
array(
"Title" => "Second table",
"Rows" => array(
array(
"Cells" => array("Col1", "Col2", "Col3")
)
)
)
)
)
);[/PHP]

What I am having a hard time understanding is exactly HOW to loop the columns. I mean, I can't put a foreach loop within an array. Or can I? :)

This code would have been perfect had I been able to loop the columns.

Can someone please explain how this could be done using this code? I am just trying to understand how this could be done.

Thanks,

Frank
Sep 7 '08 #1
Share this Question
Share on Google+
6 Replies


Markus
Expert 5K+
P: 6,050
Can you explain a little more? I'm having a hard time understanding your aim. Maybe an example would help?

You can traverse multi-dimensional arrays by nesting your loops.
Sep 7 '08 #2

100+
P: 348
fjm
Hi markusn00b,

Thanks for the help. Atli tried to help me last week on a major problem I was having trying to use php to dynamically build a report. Atli gave me the above piece of code but I couldn't get it to work. The problem I was having was that I needed to loop the rows (Sorry, my initial post stated columns, I meant rows) but I don't understand how that should work.

In this system, there are many customers. Each customer will have 1 report emailed to them every day. Each report can have multiple (Individual rows of data) compiled into it. The multidimensional arrays you see were supposed to handle the building of the tables that would deal with the multiple rows of data.

Now, as I look at the arrays where it pertains to the rows, I am totally perplexed at how I would loop them.

Arrays have never been something I was good at understanding. I have avoided them where ever I can. I see though, the more I code and the more complex my code becomes that there is no escaping using arrays. I know they are a basic part of of programming but I just have a mental block with them. :)

I hope I have explained it a little better.

Thanks for helping markusn00b.

Frank
Sep 7 '08 #3

Markus
Expert 5K+
P: 6,050
So, do you just need help looping through the array?
Sep 7 '08 #4

100+
P: 348
fjm
Yes, using that exact piece of code, how would I loop the rows? Could you provide an example say for "First table". If I wanted to use a foreach loop to do that, where would I put the loop?
Sep 7 '08 #5

pbmods
Expert 5K+
P: 5,821
Heya, Frank.

This might be useful:
Expand|Select|Wrap|Line Numbers
  1. foreach( $pages as $idxPage => $page )
  2. {
  3.   echo $idxPage, '. ', $page['Title'], '<br />';
  4.  
  5.   foreach( $page['Tables'] as $idxTable => $table )
  6.   {
  7.     echo '&nbsp;&nbsp;', $idxTable, '. ', $table['Title'], '<br />';
  8.  
  9.     foreach( $table['Rows'] as $idxRow => $row )
  10.     {
  11.         echo '&nbsp;&nbsp;&nbsp;&nbsp;Row ', $idxRow, ':<br />';
  12.  
  13.         foreach( $row['Cells'] as $idxCell => $cell )
  14.         {
  15.           echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;', $idxCell, '. ', $cell, '<br />';
  16.         } 
  17.     }
  18.   }
  19. }
  20.  
You should see something like this:
Expand|Select|Wrap|Line Numbers
  1. 1. First page
  2.   1. First table
  3.     Row 1:
  4.       1. Col1
  5.       2. Col2
  6.       3. Col3
  7.   2. Second table
  8.     Row 1:
  9.       1. Col1
  10.       2. Col2
  11.       3. Col3
  12.  
Sep 7 '08 #6

100+
P: 348
fjm
Hi Pbmods,

Thanks for helping out. :)

Ahhh....... I see.... You are looping through the array results. So physical arrays themselves do not get looped.

How embarrasing. :blush:

Thanks,

Frank
Sep 7 '08 #7

Post your reply

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