【问题标题】:combine two foreach loops结合两个 foreach 循环
【发布时间】:2012-12-26 23:48:13
【问题描述】:

我有两个 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>';
}

第二个:

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

问题是如何让这两个循环显示这样的结果:

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

代替:

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

谢谢

【问题讨论】:

  • 两个数组的大小是否相同,或者可以不同?
  • 您是否尝试过将它们嵌套在外部的“结果”循环和内部的“循环”循环?整个内部循环必须在外部循环必须递增之前完全递增。
  • $items$key 的值是多少?是0,1,2...n 还是关联数组键?

标签: php foreach


【解决方案1】:

您可以为此使用MultipleIterator

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

【讨论】:

    【解决方案2】:

    显而易见的解决方案:

    $output1 = array();
    $output2 = array();
    
    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]);
        $output1[] = '<tr><td>'.$title[$key].$infoo[$key].$item.'</tr></td>';
    }
    
    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);
        $output2[] = "<div class=\"bgframe".str_replace("/i/", "/IMG/", $a4)."</div></div>";
    }
    
    $output = array_map( null, $output1, $output2 );
    foreach ( $output as $lines ) {
        $lines = array_filter( $lines );
        foreach ( $lines as $line ) {
            echo $line;
        }
    }
    

    【讨论】:

      【解决方案3】:

      最好的答案可能来自 Jack,但我认为我的解决方案很有趣。 我还尝试改进您的代码:

      do {
          if ($item = current($items)) {
              $key = key($items);
              $keywords = str_replace(' ', '|');
              list($infoo_str, $title_str) = preg_replace(
                  "/\b({$keywords})\b(?![^<]*[>])/i",
                  '<b>$0</b>',
                  array($infoo[$key], $title[$key])
              );
      
              echo "<tr><td>{$infoo_str}{$title_str}{$item}</tr></td>";
      
              next($items);
          }
      
          if ($linkas = current($linkai)) {
              $characters = array('download/', '&', 'amp;nbsp;', '/i/');
              $replacements = array('files/', '&amp;', '&nbsp;', '/IMG/');
              $linkas = str_replace($characters, $replacements, $linkas);
      
              echo "<div class='bgframe{$linkas}'</div></div>";
      
              next($linkai);
          }
      } while ($item or $linkas);
      

      【讨论】:

        猜你喜欢
        • 2015-08-12
        • 1970-01-01
        • 2013-04-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多