结合两个foreach循环 [英] combine two foreach loops

查看:145
本文介绍了结合两个foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个foreach循环:
第一个:

  foreach($ items as $ key => $ item)
{
$ keywords = explode('',$ qsvarus);
$ title [$ key] = preg_replace('/ \ b('。implode('|',$ keywords)。')\b(?![^<] * [>]) / i','< b> $ 0< / b>',$ title [$ key]);
$ infoo [$ key] = preg_replace('/ \ b('。implode('|',$ keywords)。')\b(?![^ <] * [>]) / i','< b> $ 0< / b>',$ infoo [$ key]);
echo'< tr>< td>'。$ title [$ key]。$ infoo [$ key]。$ item。'< / tr>< / td>';



$ b第二个:

$ b pre> foreach($ linkai as $ key => $ linkas){
$ i ++;
$ a1 = $ linkas [1];
$ a2 = str_replace(download /,files /,$ a1);
$ a3 = str_replace(&,& amp;,$ a2);
$ a4 = str_replace(amp; nbsp;,nbsp;,$ a3);
echo< div class = \bgframe.str_replace(/ i /,/ IMG /,$ a4)。< / div>< / div>;





问题是如何使这两个循环显示如下结果:

  Loop1中的Result1 
Loop2中的Result1
Loop1中的Result2
Loop2中的Result2
Result3从Loop1
Result3从Loop2
等等...



pre $ Loop1中的Result1
Loop1中的Result2
Loop1中的Result3
...
Result1来自Loop2
来自Loop2的Result2
来自Loop2的Result3


解决方案

您可以使用 MultipleIterator 为此:

  $ m =新的ArrayIterator(); 
$ m-> attachIterator(new ArrayIterator($ items),'item');
$ m-> attachIterator(new ArrayItera tor($ linkai),'linkas');

foreach($ m为$单位){
// $单位['item']包含来自$ items
的单元// $ unit ['linkas']包含来自$ linkai

//你的逻辑
}


I have two foreach loops: 1st one:

        foreach ($items as $key => $item) 
        {   
        $keywords = explode(' ', $qsvarus);
        $title[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $title[$key]);
        $infoo[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $infoo[$key]);
        echo '<tr><td>'.$title[$key].$infoo[$key].$item.'</tr></td>';
}

2nd one:

foreach ($linkai as $key => $linkas) {
    $i++;
    $a1 = $linkas[1];
$a2 = str_replace("download/", "files/", $a1);
$a3 = str_replace("&","&amp;", $a2);
$a4 = str_replace("amp;nbsp;","nbsp;", $a3);
echo "<div class=\"bgframe".str_replace("/i/", "/IMG/", $a4)."</div></div>";
}

The question is how to make those two loops to display results like this:

Result1 from Loop1
Result1 from Loop2
Result2 from Loop1
Result2 from Loop2
Result3 from Loop1
Result3 from Loop2
And so on...

Instead of:

Result1 from Loop1
Result2 from Loop1
Result3 from Loop1
...
Result1 from Loop2
Result2 from Loop2
Result3 from Loop2

Thanks

解决方案

You can use the MultipleIterator for this:

$m = new MultipleIterator();
$m->attachIterator(new ArrayIterator($items), 'item');
$m->attachIterator(new ArrayIterator($linkai), 'linkas');

foreach ($m as $unit) {
    // $unit['item'] contains an entry from $items
    // $unit['linkas'] contains an entry from $linkai

    // your logic here
}

这篇关于结合两个foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆