如何将foreach循环限制为三个循环 [英] how to limit foreach loop to three loops

查看:129
本文介绍了如何将foreach循环限制为三个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制此循环..只是你的循环..感谢您的帮助

how to limit this loop ..just thee loops..thanks for helping

<?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 ?>

这篇关于如何将foreach循环限制为三个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆