Multi-Dimensional Arrays Help - And Other Questions on Arrays
Collapse
X
-
With nested foreach loops.
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
-
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
-
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
-
otherwise you better use this
Code: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
-
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..
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
-
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