Jump to content

intermix foreach loops


dreamwest

Recommended Posts

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

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.