dreamwest Posted October 6, 2009 Share Posted October 6, 2009 I have 2 seperate foreach loops is there a way i can intermix the results? foreach($search as $word){ $word; } foreach($tags as $tag){ echo $tag; } Both loop 5 times each. foreach word has: apple bannana orange peel grape foreach tag has seeds long color external round What i wanted to do was randomly intermix the 2 so it would look like something this: apple bannana seeds external orange peel long grape color round Quote Link to comment https://forums.phpfreaks.com/topic/176764-intermix-foreach-loops/ Share on other sites More sharing options...
trq Posted October 6, 2009 Share Posted October 6, 2009 <?php $a = array_rand(array_merge($search, $tags)); foreach($a as $word){ $word; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/176764-intermix-foreach-loops/#findComment-931991 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 Don't pay attention to thorpe's code. It's totally wrong and fails to deliver. he forgot to echo $word; // Quote Link to comment https://forums.phpfreaks.com/topic/176764-intermix-foreach-loops/#findComment-932011 Share on other sites More sharing options...
dreamwest Posted October 6, 2009 Author Share Posted October 6, 2009 Thanks. But ive still need to keep the 2 foreach loops for processing reasons I still cant figure out how to create an array from a loop foreach($search as $word){ $row1 .= $word; } foreach($tags as $tag){ $row2 .= $tag; } $a = array_rand(array_merge($row1, $row2)); foreach($a as $all_row){ echo $all_row; } Quote Link to comment https://forums.phpfreaks.com/topic/176764-intermix-foreach-loops/#findComment-932018 Share on other sites More sharing options...
.josh Posted October 6, 2009 Share Posted October 6, 2009 hmm...dunno why you really need 2 foreach loops...you could probably optimize your code, but whatever, not within scope of this thread. foreach($search as $word){ $a[] = $word; } foreach($tags as $tag){ $a[] = $tag; } shuffle($a); foreach($a as $all_row){ echo $all_row; } Quote Link to comment https://forums.phpfreaks.com/topic/176764-intermix-foreach-loops/#findComment-932022 Share on other sites More sharing options...
dreamwest Posted October 7, 2009 Author Share Posted October 7, 2009 hmm...dunno why you really need 2 foreach loops...you could probably optimize your code, but whatever, not within scope of this thread. foreach($search as $word){ $a[] = $word; } foreach($tags as $tag){ $a[] = $tag; } shuffle($a); foreach($a as $all_row){ echo $all_row; } Thanks - works like a charm Quote Link to comment https://forums.phpfreaks.com/topic/176764-intermix-foreach-loops/#findComment-932039 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.