объедините два цикла для каждого
У меня есть два цикла для каждого: 1-й:
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>';
}
2-й:
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 "<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
Спасибо
4
3 answers
Вы можете использовать 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
}
4
Author: Ja͢ck, 2012-12-27 00:06:19
Очевидное решение:
$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("&","&", $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;
}
}
2
Author: Ilmari Karonen, 2012-12-26 23:59:49
Лучший ответ, вероятно, от Джека, но я подумал, что мое решение было интересным. Я также попытался улучшить ваш код:
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/', '&', ' ', '/IMG/');
$linkas = str_replace($characters, $replacements, $linkas);
echo "<div class='bgframe{$linkas}'</div></div>";
next($linkai);
}
} while ($item or $linkas);
0
Author: Nicholas Ruunu, 2012-12-27 11:06:30