greenheart Posted October 7, 2009 Share Posted October 7, 2009 I have two, arrays one of titles one of descriptions. I want to echo the title in the first array followed by the first description in the second array, then echo the second title and the second description in the same way and so on up until the 10th value in each array. Can someone kindly give me an example which I can use for this? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/176885-echoing-values-from-two-different-arrays-alternating/ Share on other sites More sharing options...
cags Posted October 7, 2009 Share Posted October 7, 2009 Depends on the structure of the data, providing both arrays are the same length and it's a non-associative array... <?php $titles = array("title 1", "title 2", "title 3"); $decriptions = array ("Blah... 1", "Blah... 2", "Blah... 3"); foreach($titles as $k=$title) { echo $title; echo $descriptions[$k]; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176885-echoing-values-from-two-different-arrays-alternating/#findComment-932665 Share on other sites More sharing options...
greenheart Posted October 7, 2009 Author Share Posted October 7, 2009 Hello I get the error message unexpected '=' from the line foreach($titles as $k=$title) { When I try the example you gave me. Quote Link to comment https://forums.phpfreaks.com/topic/176885-echoing-values-from-two-different-arrays-alternating/#findComment-932685 Share on other sites More sharing options...
salathe Posted October 7, 2009 Share Posted October 7, 2009 With regards to the error, it should be => not just = You could instead make use of the SPL (specifically the ArrayIterator and MultipleIterator) to loop over both arrays at the same time. For example: <?php $titles = new ArrayIterator(array("title 1", "title 2", "title 3")); $descriptions = new ArrayIterator(array("Blah... 1", "Blah... 2", "Blah... 3")); // Use a MultipleIterator to iterate over our two arrays at the same time! $mit = new MultipleIterator(MultipleIterator::MIT_KEYS_ASSOC); $mit->attachIterator($titles, 'title'); $mit->attachIterator($descriptions, 'desc'); foreach ($mit as $it) { echo '<h1>' . $it['title'] . "</h1>\n"; echo '<p>' . $it['desc'] . "</p>\n\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176885-echoing-values-from-two-different-arrays-alternating/#findComment-932688 Share on other sites More sharing options...
greenheart Posted October 7, 2009 Author Share Posted October 7, 2009 Thanks, that correction solves the error and it works! The ArrayIterator and MultipleIterator is a good tip for me, I'm going to be doing a fair bit of iterating over multiple arrays. Quote Link to comment https://forums.phpfreaks.com/topic/176885-echoing-values-from-two-different-arrays-alternating/#findComment-932694 Share on other sites More sharing options...
cags Posted October 7, 2009 Share Posted October 7, 2009 Btw, the error with my code was down to a typo, it should have been $k=>$title, this basically creates $k as the key of the array and $title as each value, I used that logic to fetch the item out of the other array with the same index. Salthe's solution is possibly the 'bettter' solution though. Quote Link to comment https://forums.phpfreaks.com/topic/176885-echoing-values-from-two-different-arrays-alternating/#findComment-932708 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.