Php 跳出foreach循环,回显,然后继续foreach?

Php 跳出foreach循环,回显,然后继续foreach?,php,foreach,Php,Foreach,我有一小段脚本,它只运行一个查询,检索数据库中的所有类别,然后一个接一个地列出它们 if ($all_categories) { foreach ($all_categories as $cid => $arr) { $sidebar .= '<a href="index.php?action=sort&cid=' . $cid . '">' . $arr['name'] . ' (' . $arr['count'] . ')</a><br /&

我有一小段脚本,它只运行一个查询,检索数据库中的所有类别,然后一个接一个地列出它们

if ($all_categories) {
 foreach ($all_categories as $cid => $arr) {

 $sidebar .= '<a href="index.php?action=sort&cid=' . $cid . '">' . $arr['name'] . ' (' . $arr['count'] . ')</a><br />';
 }
}
} else {
$sidebar = 'There are no categories yet';
}
if($all_类别){
foreach($cid=>$arr的所有类别){
$sidebar.='
'; } } }否则{ $sidebar='还没有分类'; }
基本上,我需要这段代码循环前10个结果,然后回显一些HTML div,然后在循环中重新提取。然而,我是新的,不知道我将如何去做这件事。我正在考虑合并一个计数器,但不确定这是否是正确的方法。

if($all_categories){
if ( $all_categories ) {
    $count = 1;

    foreach ( $all_categories as $cid => $arr ) {
        $sidebar .= '<a href="index.php?action=sort&cid=' . $cid . '">' . $arr['name'] . ' (' . $arr['count'] . ')</a><br />';

        if ( 10 == $count ) {
            $sidebar .= 'This code fires after the 10th result is displayed.';
        }

        $count++;
    }
} else {
    $sidebar = 'There are no categories yet';
}
$count=1; foreach($cid=>$arr的所有类别){ $sidebar.='
'; 如果(10==$count){ $sidebar.='此代码在显示第10个结果后激发'; } $count++; } }否则{ $sidebar='还没有分类'; }
计数器是一个不错的选择。试试看。$count>0&&$count++%10==0更好这个问题指的是前10个结果,而不是每10个结果。很可能他实际上是在谈论每10个结果,其他一切都没有多大意义。我需要$sidebar来接管回音。所以边栏打印10个类别,然后边栏打印div代码,然后边栏完成类别。也不是,在第10个结果之后不是每10个。@user3858124-我已经更新了代码,将其附加到侧栏变量中,而不是直接回显。