469,326 Members | 1,548 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,326 developers. It's quick & easy.

Multi-Dimensional Arrays Help - And Other Questions on Arrays

348 100+
For some reason, I have always had a hard time understanding arrays as they pertain to php and databases. I understand associative arrays just fine but when there are multidimensional arrays, I kinda don't.

I have gone over a few different examples but they were limited. I was able to find one piece of code that I would like to disect and ask questions about so I can gain a better understanding.

Expand|Select|Wrap|Line Numbers
  1. $characters = array
  2. (
  3.   array ( name=>"name 1"
  4.   , occupation=>"developer"
  5.   , age=>30
  6.   , specialty=>"Java"
  7.   ),
  8.   array
  9.   (
  10.     name=>"name 2"
  11.     , occupation=>"Programmer"
  12.     , age=>24
  13.     , specialty=>"C++"
  14.   ),
  15.   array
  16.   (
  17.   name=>"name 3"
  18.   , occupation=>"designer"
  19.   , age=>63
  20.   , specialty=>"Javascript"
  21.   )
  22. );
  23.  
  24. foreach ($characters as $val)
  25. {
  26.   foreach ($val as $key=>$final_val)
  27.   {
  28.     print "$key: $final_val<br>";
  29.   }
  30.   print "<br>";
  31. }
In this code, the way I am reading this is that there are 3 "rows"??? or blocks of data. Each one of these rows or blocks has several other rows inside of it. I don't have a problem with the arrays per se but more the foreach loop. If I am incorrect about these 3 arrays being rows, please feel free to correct me.

On the foreach, can someone please tell me exactly how and why it is set up the way it is? Specifically, I don't understand why the coder didn't use a key/value pair. He only uses a value, then inside the loop he uses $key=>$final_val. What I need to understand is why and when to refer to or use the key value pair and when not to. I have also seen code written inside the foreach loop like so: $key['something'] = $val;

What is that? What exactly does it do? If any one can help me to understand these, I would be forever grateful. I have pulled my hair out for the last time on drawing my data out of an array.

Thanks.
Apr 21 '09 #1
110 5621
Dormilich
8,651 Expert Mod 8TB
firstly, the posted code should give you quite a bunch of error notices (the keys are set as constants, which I doubt are intended)

if you're familiar with Maths, imagine arrays as matrices where you can access each element with the appropriate index (aka key) values (like $array[$i][$j][$k]). the sample would correspond to a 3x4 matrix.

@fjm
the reason for not using a key/value pair in the first loop is, that the keys are not needed by the program/coder (they are numeric values). in the first foreach he loops through the topmost array getting an array as value ($val) (gettype($val) would give you "array") then he loops over that value/this array and prints the keys ($key) and values ($final_val) of this sub-array.

foreach can be used in 2 ways, so you can use the syntax you really need:
Expand|Select|Wrap|Line Numbers
  1. // values only
  2. foreach ($array as $value)
  3.  
  4. //keys and values
  5. foreach ($array as $key => $value)
@fjm
it depends on what you want to do, if you don't need the keys you omit them.

@fjm
looks strange, I need to see the whole code (part) to explain.
Apr 21 '09 #2
Ciary
247 Expert 100+
@Dormilich
shouldn't that be something like this?
Expand|Select|Wrap|Line Numbers
  1. $array[$key]=$val;
or for an associative array:
Expand|Select|Wrap|Line Numbers
  1. $array['something']=$val
in this case, it's used to write to the array in a foreach loop to change its values. next is a multidimentional example based on your array.

Expand|Select|Wrap|Line Numbers
  1. $i = 0;
  2. foreach($array as $key => $val){
  3.    $val[0] = "changedname " + $i;
  4.    $i ++;
  5.    $array[$key] = $val;
  6. }
this code changes the first value on each row to "changedname " and the row number. to do this you need a key but mostly you wont use it. mostly, you need it when you want to write to the array.

the code as you wrote it cant be right. it states that your $key is an array. or it could be a foreign library i dont know about.
Apr 21 '09 #3
fjm
348 100+
@Dormilich
Hey Dormilich, thanks for the help. Actually, I ran the code and I don't have any errors or notices and my server is set for E_ALL.

@Dormilich
I'm thinking in terms of tables here. Are you saying that your sample would correspond to a table with 1 row and 4 cells? That's what I am seeing.

@Dormilich
Since I have posted, I have been looking for that code sample that you didn't seem to understand as well as playing with MD arrays. BTW: Ciary was exactly correct on the code he posted. Please see his example for what I was trying to convey.

In almost everything I do, I will always have my own key. I don't need php to generate the numeric key for me. So, that being the case, would a foreach loop be written any differently from the code that I posted to get my key?

@Dormilich
What I understand from your example is that the first foreach you wrote would be good for an assoc array and the second would be for a MD array. Yes?
Apr 21 '09 #4
fjm
348 100+
@Ciary
Hi Ciary. That's cool! Thanks for the explanation.

Here is what has been happening that has been making me pull my hair out for the past 7 months with regard to arrays. I have a class that gets my results from my database. The method uses mysql_fetch_array and returns the values. Well, along with my key/value pairs, would be an identical, duplicate row of data with the array integer and I could never seem to figure out how to omit the int values. As it turned out, the mysql_fetch_array() in my method used "MYSQL_BOTH" and was the culprit. I changed it to MYSQL_ASSOC and I have been golden ever since. I still want to learn more about arrays though because I have reached a point of understanding where I see that arrays are really helpful in transporting my data in bulk and I can work with it and do whatever I want with it.
Apr 21 '09 #5
Dormilich
8,651 Expert Mod 8TB
@fjm
well, I got the expected 12 notices…

@fjm
for a two-dimensional array you can think in terms of tables. but it would be a 3(4) row / 4(3) column table (depending upon where you set the precedence).

@fjm
No, the first is if you're only interested in the values. the type of the keys doesn't matter at all.
Apr 21 '09 #6
Dormilich
8,651 Expert Mod 8TB
@fjm
as described in the manual. if you run into such problems, it is always a good idea to read all of the manual entry (even the comments). this solves most of these problems
Apr 21 '09 #7
Ciary
247 Expert 100+
you're welcome :)

to answer the other question:

a matrix looks like a table with rows and columns. actually thats how arrays work 2. but you also have more dimensions as 2. good example:
Expand|Select|Wrap|Line Numbers
  1. $_SESSION["foo"][][][]
$_SESSION is a standard array provided by php. you can put 3 dimentional array in it. in that way, you have a 4D array(there isn't a way to visualize this though).
i hope this isnt making it 2 difficult.

if you already have the key wihout the foreach. you dont need the foreach
example: i want to change the element on row 4 column 8:
Expand|Select|Wrap|Line Numbers
  1.    $array[3][7] = $val;
  2.  
note that an array starts at 0 so thats why i lowered my indexes by 1

for the different ways of foreach:
use the one with the key to write and the one without the key to read unless you need the key for something else.

i hope i havent complicated things :)

EDIT: srr for the double post Dormilich. seems like we were posting at the same time
Apr 21 '09 #8
fjm
348 100+
@Dormilich
Well, let's just say that I'm hard headed and sometimes hate to read. Honestly though, I would not have known about that if someone would not have pointed it out to me.

I really had no idea why I was getting duplicated rows. Now that I have read the manual on the fetch_array function, I feel more comfortable now and can finally concentrate on understanding arrays more. I could never figure out if it was me writing a bad loop or if something else was going on or both.
Apr 21 '09 #9
fjm
348 100+
@Dormilich
From my ini file: error_reporting = E_ALL & ~E_NOTICE | E_STRICT
I just ran the code again and got nothing. Completely clean. I'm on version 5x

@Dormilich
OK so if I understand.. An array over 2D doesn't look like a table? Sometimes I need to visualize something to understand it. I'm also not that familier with maths. I did manage to find a matrix and it resembled a table, kind of.
Apr 21 '09 #10
fjm
348 100+
@Ciary
Maybe I have a better example. Are these MD arrays kind of like nested tables? If yes, then I can certanly picture it. :)
Apr 21 '09 #11
Ciary
247 Expert 100+
a table is a 2 dimentional array. and all other MD arrays are exacly as nested tables :)
Apr 21 '09 #12
Dormilich
8,651 Expert Mod 8TB
@fjm
that means: report all errors except notices and compatibility warnings

there's a whole section explaining this *nudgenudge*

@fjm
well, yes. although it gets complicated for 3-dimensionals and unimaginable for more than 4 dimensions.
Apr 21 '09 #13
fjm
348 100+
Wow guys... OK... So let me recap... So we have a 2D array that resembles a table. Then we have a higher level of dimensions that we can say looks like nested tables. Kind of like sayins that each cell can have an array inside of it and it just keeps going from there. Maybe an example.

Expand|Select|Wrap|Line Numbers
  1. <table>  <-- array 1
  2.   <tr>
  3.     <td>
  4.       <table>  <-- array 2
  5.         <tr>
  6.           <td>
  7.             <table>  <-- array 3
  8.               <tr>
  9.                 <td>
  10.                   Etc..
  11.                 </td>
  12.               </tr>
  13.             </table>
  14.           </td>
  15.         </tr>
  16.       </table> 
  17.     </td>
  18.   </tr>
  19. </table>
  20.  
Am I correct? 3D array?
Apr 21 '09 #14
fjm
348 100+
@Dormilich
OK, you win. :) Actually, yesterday I wrote some code and knew that I should have gotten an undefined index but didn't. I was wondering why and figured that it had something to do with the new changes I made.

It's really strange because with those ini values, I now get new messages that I didn't get before like the usual date() timezone error so I thought that it was a stricter ruleset. Thanks for the heads up Dormilich! If I wanted to have a strict set of rules, would I just ise E_ALL? (I'll still read the link you provided.)
Apr 21 '09 #15
Dormilich
8,651 Expert Mod 8TB
@fjm
that's the crucial point.
Apr 21 '09 #16
Dormilich
8,651 Expert Mod 8TB
@fjm
yup.

(blablablubb…20 character limit exeeded…narf)
Apr 21 '09 #17
Ciary
247 Expert 100+
take a look at this: its a visual example of a 4D array. hope this helps.


and indeed you create it by putting an array in a array. and that one in another array and that one in another and ... (you get the poin :) )
Apr 21 '09 #18
fjm
348 100+
@Dormilich
Scary thought because each cell or array can have an infinite number of arrays. It blows my mind to even imagine it.

Ok, so if I can just bring this into perspective and maybe have you guys guide me a bit now that I can visualize it. Let's say I have an array like this:

Expand|Select|Wrap|Line Numbers
  1. array
  2. (
  3.   array
  4.   (
  5.     'mykey0' => 'myvalue0',
  6.     'mykey1' => 'myvalue1',
  7.     'mykey2' => 'myvalue2'
  8.   ),
  9.   array
  10.   (
  11.     '0' => 'somthin',
  12.     '1' => 'somthin',
  13.     '2' => 'somthin',
  14.     array('0' =>'somethin here')
  15.   )
  16. )
  17.  
OK. I have a MD array here with a nested array. (Not sure what to call it) How would I refer to the nested array with a foreach?
Apr 21 '09 #19
fjm
348 100+
@Ciary
Great example Ciary, thanks. Is this a 4D array because each box has 4 columns or because each box has 4 columns? How did you determine that it was 4?

Please tell me that I wouldn't ever encounter an array like this. :)
Apr 21 '09 #20
Dormilich
8,651 Expert Mod 8TB
@fjm
you haven't seen where such array are used in reality…
@fjm
call it MD or nested array.
@fjm
which one? you have a total of 4 arrays…
Expand|Select|Wrap|Line Numbers
  1. $array
  2. $array[0]
  3. $array[1]
  4. $array[1][0]
although you might run into problems here:
Expand|Select|Wrap|Line Numbers
  1. array('0' => "value", "value")
because array(0 => x) and array(x) is the same and PHP is not strongly typed (i.e. it can easily switch variable types and the difference between 0 and '0' is not much)
Apr 21 '09 #21
fjm
348 100+
Expand|Select|Wrap|Line Numbers
  1. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant name - assumed 'name' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 4
  2. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant occupation - assumed 'occupation' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 5
  3. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant age - assumed 'age' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 6
  4. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant specialty - assumed 'specialty' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 7
  5. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant name - assumed 'name' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 11
  6. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant occupation - assumed 'occupation' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 12
  7. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant age - assumed 'age' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 13
  8. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant specialty - assumed 'specialty' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 14
  9. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant name - assumed 'name' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 18
  10. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant occupation - assumed 'occupation' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 19
  11. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant age - assumed 'age' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 20
  12. [Tue Apr 21 02:23:06 2009] [error] [client 127.0.0.1] PHP Notice:  Use of undefined constant specialty - assumed 'specialty' in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\acme\\3.php on line 21
  13.  
hehe.. Thanks Dormilich, my errors are now back!
Apr 21 '09 #22
Ciary
247 Expert 100+
Expand|Select|Wrap|Line Numbers
  1. foreach($array as $val){
  2.    if($val[3]){
  3.       echo $val[3][0];    //this will print 'somethin here'
  4.    }
  5. }
the 'if' is very important otherwise you will get an error because you try to echo something that doesnt exist.
Apr 21 '09 #23
Dormilich
8,651 Expert Mod 8TB
@fjm
the 4x5 (5x4) tables are 2D, then you have 3 rows making it 3D and 4 columns making 4D.
Apr 21 '09 #24
Ciary
247 Expert 100+
it has nothing to do with row/column count. it's purely the number of dimentions. or better: the dept of the nested array.
Expand|Select|Wrap|Line Numbers
  1. $array = array(array(array(array())))
this is a 4D array
Apr 21 '09 #25
fjm
348 100+
@Dormilich
You're right. In every day practice, I couldn't see anything like that; thank God. I think that the most I could see would be a 2D or maybe a 3D array but I wouldn't think that I would have one over that.

Maybe a better example would be a 2D array.

Expand|Select|Wrap|Line Numbers
  1. $result = array
  2. (
  3.   array
  4.   (
  5.     'mykey0' => 'myvalue0',
  6.     'mykey1' => 'myvalue1',
  7.     'mykey2' => 'myvalue2'
  8.   ),
  9.   array
  10.   (
  11.     '0' => 'somthin',
  12.     '1' => 'somthin',
  13.     '2' => 'somthin',
  14.     '3' => 'somthin else'
  15.   )
  16. )
Say that I wanted to get the value "somthin else".... Could I do..

Expand|Select|Wrap|Line Numbers
  1. foreach($result as $key => $val)
  2. {
  3.   echo $key['3'][$val];
  4. }
  5.  
Would this work?
Apr 21 '09 #26
fjm
348 100+
@Ciary
OK, thanks for that Ciary.. So it simply the number of "array"(s) in the statement. Yes? You say dimensions and I am thinking code block. These fancy terms just screw me up sometimes. :)

Atli once helped me and gave me an array to do something that I needed. "WILD" stuff, man.. It was 6 or 7 arrays (dimensions). I flipped out just looking at it.
Apr 21 '09 #27
Markus
6,050 Expert 4TB
With nested foreach loops.

Expand|Select|Wrap|Line Numbers
  1. foreach ( $array as $inner )
  2. {
  3.     foreach ( $inner as $key => $val )
  4.     {
  5.         if ( is_array($val) )
  6.         {
  7.             foreach( $val as $key => $val2 )
  8.             {
  9.                 echo "{$key} => {$val2}<br />";
  10.             }
  11.         }
  12.         else
  13.         {
  14.             echo "{$key} => {$val}<br />";
  15.         }
  16.     }
  17. }
Sometimes hardcoding the loops will be out-of-the-question, so you'll need to a look see at recursion.

- mark.
Apr 21 '09 #28
Dormilich
8,651 Expert Mod 8TB
@fjm
no, $key is either string or integer (read here).

use
Expand|Select|Wrap|Line Numbers
  1. $result[1]['3']
Apr 21 '09 #29
fjm
348 100+
I found it! Here is the array that Atli gave me that just flipped me out.

Expand|Select|Wrap|Line Numbers
  1. $pages = array(
  2.     array(
  3.         "Title" => "First page",
  4.         "Tables" => array(
  5.             array(
  6.                 "Title" => "First table",
  7.                 "Rows" => array(
  8.                     array(
  9.                         "Cells" => array("Col1", "Col2", "Col3")
  10.                     ),
  11.                 )
  12.             ),
  13.             array(
  14.                 "Title" => "Second table",
  15.                 "Rows" => array(
  16.                     array(
  17.                         "Cells" => array("Col1", "Col2", "Col3")
  18.                     )
  19.                 )
  20.             )
  21.         )
  22.     )
  23. );
To me.. If I can master getting data back out of this monster, I will be very happy in my newly found knowledge of arrays.
Apr 21 '09 #30
Ciary
247 Expert 100+
@fjm
not like that: more like this.
Expand|Select|Wrap|Line Numbers
  1. foreach($result as $key => $val)
  2. {
  3.    if($val[3]){                 //very important like i said before
  4.       echo $var[3];
  5.    }
  6. }
  7.  
or like dormilich posted (to much posters, can't keep up)
Apr 21 '09 #31
Dormilich
8,651 Expert Mod 8TB
@fjm
well, looks like a "simple" array representation of a html/DOM table… *g*
Apr 21 '09 #32
fjm
348 100+
@Markus
Hi Mark. Thank you very much for that example and the link on recursion. I am skimming over it now. I wouldn't think that I would need anything over a 3D array but you never know.

Question: I noticed that you have 2 or 3 foreach loops in your example. Would it be safe to say that I need a foreach loop for each array?
Apr 21 '09 #33
fjm
348 100+
@Ciary
Ahha.. OK, I did miss that in a prior post. Thanks for bringing that to my attention again. So I need to test for the key first.. If its there then echo it.
Apr 21 '09 #34
Dormilich
8,651 Expert Mod 8TB
@Ciary
there's hardly any time to breath inbetween…
Apr 21 '09 #35
fjm
348 100+
@Dormilich
That's exactly what it is. :) Only problem was, at that time, I totally didn't get arrays and anything over an associative array sent me running in the other direction. I don't know why but arrays were freaky things for me. I mean, they are really not that bad now that you guys have taken the time to explain them to me.

I feel that I still do need some help though in learning how to gall the data back out of an array.
Apr 21 '09 #36
fjm
348 100+
@Dormilich
Its so fun though. :P
Apr 21 '09 #37
Ciary
247 Expert 100+
if you need multiple elements from each array, you do need 1 foreach for each array. except if it contains multiple element types. then, you will probably know what it contains and how large it is.

in that case use $Arr['name'] or something like that.

EDIT nearly 40 posts on this threat. lets post till we reach 50 :)
Apr 21 '09 #38
fjm
348 100+
@Ciary
lol... OK, I'm down... I have no other life anyway. My wife hates me and the cat won't come to anymore so its just me and the good ol' puter. :)

Can you explain what you mean about "if I need more elements"? I think you mean if I need more values from the array?
Apr 21 '09 #39
fjm
348 100+
Would you guys mind going over Atli's array? I wouldn't mind using it as an example. Even with all the help you guys are giving me, I am still a little freaked out by this code. How would I even get my data into it?

There are 11 arrays in there all together.
Apr 21 '09 #40
Ciary
247 Expert 100+
@fjm
exacly :) but you mostly use foreach if all values are the same type (like arrays) and you dont know the boundaries of your array.

otherwise you better use this
Expand|Select|Wrap|Line Numbers
  1. foreach($result as $val){
  2. echo $val['name'].": ".$val['points']['math']
  3. }
this will return the math results for every student

i hope this clearifies things

and i'm sorry about your cat disliking you :( :p
Apr 21 '09 #41
fjm
348 100+
Thanks Ciary, Yes, it does help. Your saying that all the values need to be inside of an array. (At least I think that's what you are saying).

Yeah, the cat just wants attention and gets mad when I am busy and can't play. She just plops herself in front of me and now I got loads of cat fur stuck to the monitor. :)
Apr 21 '09 #42
Ciary
247 Expert 100+
Expand|Select|Wrap|Line Numbers
  1.  
  2.    1. $pages = array(
  3.    2.     array(
  4.    3.         "Title" => "First page",
  5.    4.         "Tables" => array(
  6.    5.             array(
  7.    6.                 "Title" => "First table",
  8.    7.                 "Rows" => array(
  9.    8.                     array(
  10.    9.                         "Cells" => array("Col1", "Col2", "Col3")
  11.   10.                     ),
  12.   11.                 )
  13.   12.             ),
  14.   13.             array(
  15.   14.                 "Title" => "Second table",
  16.   15.                 "Rows" => array(
  17.   16.                     array(
  18.   17.                         "Cells" => array("Col1", "Col2", "Col3")
  19.   18.                     )
  20.   19.                 )
  21.   20.             )
  22.   21.         )
  23.   22.     )
  24.   23. );
  25.  
  26.  
first, he has an associative array $pages. in it is an array which always contains 2 elements: a title and an array of tables.

the array tables will also contains another array with 2 element (this happens a lot). one with the title and one with the rows of that table. each row in rows is also an array containing the cells on that row..

i think thats about it. hope you understand it now. it's only a 7D associative array :)
Apr 21 '09 #43
fjm
348 100+
OK, so taking Atli's array..

Expand|Select|Wrap|Line Numbers
  1. $pages = array
  2. (
  3.   array
  4.   (
  5.     "Title" => "First page",
  6.     "Tables" => array
  7.     (
  8.       array
  9.       (
  10.         "Title" => "First table",
  11.         "Rows" => array
  12.         (
  13.           array
  14.           (
  15.             "Cells" => array
  16.             (
  17.               "Col1", "Col2", "Col3"
  18.             )
  19.           ),
  20.         )
  21.       ),
  22.       array
  23.       (
  24.         "Title" => "Second table",
  25.         "Rows" => array
  26.         (
  27.           array
  28.           (
  29.             "Cells" => array
  30.             (
  31.               "Col1", "Col2", "Col3"
  32.             )
  33.           )
  34.         )
  35.       )
  36.     )
  37.   )
  38. );
Here is the way I am reading this. Please correct me if I am wrong. I count a total of 11 arrays in this code. Can we say that this is an 11 dimension array?

I see that the outermost array holds all others. The next one in holds the tables. The next one in holds the actual table data. The next one holds the rows. The next one holds the actual row data. The next one holds the cells and the last array holds the actual cell data. Am I correct???
Apr 21 '09 #44
Dormilich
8,651 Expert Mod 8TB
@Ciary
I need to butt in here, but
the first array is numeric and holds the "pages", the second array is associative and holds the info about the actual page (i.e. title and table)
Apr 21 '09 #45
Dormilich
8,651 Expert Mod 8TB
@fjm
nope, the depth is 7, there are some arrays on the same level.
Apr 21 '09 #46
Ciary
247 Expert 100+
partly:

the root element holds pages.
the page holds a title and tables.
the tables holds another title and rows.
rows holds array cells
and each element in cells contains data.

try visualizing it: it's a page with 2 tables. both have a title and the page also has a title. each table contains 1 row with 3 elements (cells)

indeed the root isn't associative but all other arrays are.

i understand your confusion but notice that the array tables contains 2 tables. but they're not nested. this makes it a 7 dimension array and not an 11 dimension one :)

EDIT as dormilich just stated :)
Apr 21 '09 #47
Dormilich
8,651 Expert Mod 8TB
@Ciary
"Tables", "Rows" and "Cells" are numeric too.
Apr 21 '09 #48
Markus
6,050 Expert 4TB
Jee, it's hard to keep up :D

Um, you'll need a foreach loop for every new level of arrays.

Expand|Select|Wrap|Line Numbers
  1. $array1 = array ( 0 => 0, 1 => 1, 2 => 2 );
  2. // Would require one loop.
  3.  
  4. $array2 = array ( 0 => 1, 1 => array ( 0 => 0, 1 => 1 ), 2 => 2 );
  5. // Would require two loops.
  6.  
Good luck with not going brain-dead after this thread.

- mark (is a poet. ;)
Apr 21 '09 #49
Ciary
247 Expert 100+
nope, only "cells". all other contain named elements.

EDIT: WOOHOO 50 posts :)
Apr 21 '09 #50

Post your reply

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

Similar topics

4 posts views Thread by OutsiderJustice | last post: by
37 posts views Thread by ajikoe | last post: by
4 posts views Thread by Frank Jona | last post: by
23 posts views Thread by Kaz Kylheku | last post: by
17 posts views Thread by =?Utf-8?B?R2Vvcmdl?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.