我有两个foreach循环:
第一个:
foreach ($items as $key => $item)
{
$keywords = explode(' ', $qsvarus);
$title[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^])/i', '$0', $title[$key]);
$infoo[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^])/i', '$0', $infoo[$key]);
echo '
'.$title[$key].$infoo[$key].$item.'';}
第二个:
foreach ($linkai as $key => $linkas) {
$i++;
$a1 = $linkas[1];
$a2 = str_replace("download/", "files/", $a1);
$a3 = str_replace("&","&", $a2);
$a4 = str_replace("amp;nbsp;","nbsp;", $a3);
echo "
}
问题是如何使这两个循环显示如下结果:
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
谢谢
解决方法:
$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
}
标签:php,foreach
来源: https://codeday.me/bug/20190529/1180239.html