amanxman Posted June 28, 2012 Share Posted June 28, 2012 Hi, Say I have three sets of data all related, at the moment I am putting each 'set' in to it's own array and doing this: $firstArray = array("a", "b", "c"); $secondArray = array ("1", "2", "3"); $thirdArray = array ("apple", "banana", "candy"); // to output foreach ($firstArray as $key => $value) { echo $value; echo $secondArray[$key]; echo $thirdArray[$key]; } // returns "a1apple", "b2banana, "c3candy" I'm thinking there must be a better (more efficient) way of doing this, by storing the 3 sets (1, a, apple) into one multidimensional array, so output is something like: foreach ($multiArray as $value) { echo $value; echo $value[secondlevel]; echo $value[thirdlevel]; } I've read a bit on multidimensional arrays, but can't work it out... Is there a better way? Thanks in advance, much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/264965-nestedmulti-dimension-array/ Share on other sites More sharing options...
xyph Posted June 28, 2012 Share Posted June 28, 2012 Something like this? <?php $data = array( array("1", "a", "apple"), array("2", "b", "banana"), array ("3", "c", "candy") ); ?> and <?php $data = array( array("1", "a", "apple"), array("2", "b", "banana"), array ("3", "c", "candy") ); foreach( $data as $item ) { echo "{$item[0]}, {$item[1]}, {$item[2]}<br>"; } ?> ? Quote Link to comment https://forums.phpfreaks.com/topic/264965-nestedmulti-dimension-array/#findComment-1357789 Share on other sites More sharing options...
amanxman Posted June 28, 2012 Author Share Posted June 28, 2012 Awesome, many thanks... Knew it'd be simple (but just didn't know!!!) Cheers again Quote Link to comment https://forums.phpfreaks.com/topic/264965-nestedmulti-dimension-array/#findComment-1357790 Share on other sites More sharing options...
Barand Posted June 28, 2012 Share Posted June 28, 2012 <?php foreach( $data as $item ) { echo "{$item[0]}, {$item[1]}, {$item[2]}<br>"; } ?> or <?php $data = array( array("1", "a", "apple"), array("2", "b", "banana"), array ("3", "c", "candy") ); foreach( $data as $arr ) { echo join(', ', $arr) . '<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264965-nestedmulti-dimension-array/#findComment-1357799 Share on other sites More sharing options...
amanxman Posted June 28, 2012 Author Share Posted June 28, 2012 Thanks Second one looks easier for lots and lots of levels... Quote Link to comment https://forums.phpfreaks.com/topic/264965-nestedmulti-dimension-array/#findComment-1357802 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.