jayfray84 Posted February 20, 2011 Share Posted February 20, 2011 Hi there, I have the following PHP code: <?php $books = array( array("title"=>"Book Title 1", "author"=>"Author 1", "publisher"=>"Publisher 1"), array("title"=>"Book Title 2", "author"=>"Author 2", "publisher"=>"Publisher 2"), array("title"=>"Book Title 3", "author"=>"Author 3", "publisher"=>"Publisher 3"), ); foreach ($books as $key => $value) { echo "$key : $value<br>"; } ?> I'm trying to loop through this code, but all it outputs is: 0: ARRAY 1: ARRAY 2: ARRAY -- It's not even pulling the proper $keys and $values. It works when there's just ONE array -- but not with this multidimensional array. Any ideas? Thanks! J Quote Link to comment https://forums.phpfreaks.com/topic/228251-simple-question-looping-an-array/ Share on other sites More sharing options...
jcbones Posted February 20, 2011 Share Posted February 20, 2011 <?php $books = array( array("title"=>"Book Title 1", "author"=>"Author 1", "publisher"=>"Publisher 1"), array("title"=>"Book Title 2", "author"=>"Author 2", "publisher"=>"Publisher 2"), array("title"=>"Book Title 3", "author"=>"Author 3", "publisher"=>"Publisher 3"), ); foreach ($books as $key => $value) { foreach($value as $kk => $vv) { echo "$kk : $vv<br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/228251-simple-question-looping-an-array/#findComment-1177037 Share on other sites More sharing options...
jayfray84 Posted February 20, 2011 Author Share Posted February 20, 2011 That did it -- thank you --- Can you tell me WHY that worked? You added that extra bit there...... Thanks so much, J Quote Link to comment https://forums.phpfreaks.com/topic/228251-simple-question-looping-an-array/#findComment-1177039 Share on other sites More sharing options...
jcbones Posted February 20, 2011 Share Posted February 20, 2011 Yes, books is a multidimensional array. That means you have an array, nested inside of another array. You can have as many dimensions as you need, however, at this juncture you have 2 dimensions. So, your first foreach loop, goes through the first dimension, and looks at each key (starts at 0). This is why your numbers start as 0 - 2, however now your values are returning array, this is because the values are the second dimension of your array, or as some call it, nested. The second foreach loop, goes through the second dimension of your arrays, now we can access the keys (title, author, publisher) and the values. Since your array doesn't have any more dimensions, these values are not ARRAY's but STRINGS. You can echo out strings. Quote Link to comment https://forums.phpfreaks.com/topic/228251-simple-question-looping-an-array/#findComment-1177043 Share on other sites More sharing options...
jayfray84 Posted February 20, 2011 Author Share Posted February 20, 2011 Thank you!!! I really appreciate the quick help J Quote Link to comment https://forums.phpfreaks.com/topic/228251-simple-question-looping-an-array/#findComment-1177045 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.