php – 如何限制foreach循环到三个循环

前端之家收集整理的这篇文章主要介绍了php – 如何限制foreach循环到三个循环前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何限制这个循环..调整你的循环…帮助
<?PHP
    foreach($section['Article'] as $article) :
?>
<tr>
    <td>
        <?PHP
            if ($article['status'] == 1) {
                echo $article['title'];
            } 
        ?>
    </td>
    <td>
        <?PHP
            if($article['status']== 1) {
                echo '&nbsp;'.$html->link('View','/articles/view/'.$article['id']);
            }
        ?>
    </td>
</tr>
<?PHP 
    endforeach; 
?>
首先,准备你的数据
$i = 1;
$data = array();
foreach($section['Article'] as $article ) {
  if($article['status']== 1) {
    $article['link'] = $html->link('View','/articles/view/'.$article['id']);
    $data[] = $article;
    if ($i++ == 3) break;
  }
}
$section['Article'] = $data;

然后显示

<?PHP foreach($section['Article'] as $article ): ?>
<tr>
  <td><?PHP echo $article['title'] ?></td>
  <td>&nbsp;<?PHP echo $article['link']?></td>
</tr>
<?PHP endforeach ?>
原文链接:https://www.f2er.com/php/131564.html

猜你在找的PHP相关文章