Multi-Dimensional Arrays Help - And Other Questions on Arrays
Collapse
X
-
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.With nested foreach loops.
Sometimes hardcoding the loops will be out-of-the-question, so you'll need to a look see at recursion.Code:foreach ( $array as $inner ) { foreach ( $inner as $key => $val ) { if ( is_array($val) ) { foreach( $val as $key => $val2 ) { echo "{$key} => {$val2}<br />"; } } else { echo "{$key} => {$val}<br />"; } } }
- mark.
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?Comment
-
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.Comment
-
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.Comment
-
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 :)Comment
-
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. :)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 :)
Can you explain what you mean about "if I need more elements"? I think you mean if I need more values from the array?Comment
-
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
this will return the math results for every studentCode:foreach($result as $val){ echo $val['name'].": ".$val['points']['math'] }
i hope this clearifies things
and i'm sorry about your cat disliking you :( :pComment
-
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. :)Comment
-
first, he has an associative array $pages. in it is an array which always contains 2 elements: a title and an array of tables.Code: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. );
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 :)Comment
-
OK, so taking Atli's array..
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?Code:$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" ) ) ) ) ) ) );
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???Comment
-
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)Comment
Comment