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
https://forums.phpfreaks.com/topic/176764-intermix-foreach-loops/
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;
}

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.