如何限制这个循环..只是你循环..谢谢你的帮助
<?php
foreach($section['Article'] as $article) :
?>
<tr>
<td>
<?php
if ($article['status'] == 1) {
echo $article['title'];
}
?>
</td>
<td>
<?php
if($article['status']== 1) {
echo ' '.$html->link('View', '/articles/view/'.$article['id']);
}
?>
</td>
</tr>
<?php
endforeach;
?>
Run Code Online (Sandbox Code Playgroud)
Ign*_*ams 85
切片阵列.
foreach(array_slice($section['Article'], 0, 3) as $article ):
Run Code Online (Sandbox Code Playgroud)
You*_*nse 30
首先,准备你的数据
$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;
Run Code Online (Sandbox Code Playgroud)
然后显示它
<?php foreach($section['Article'] as $article ): ?>
<tr>
<td><?php echo $article['title'] ?></td>
<td> <?php echo $article['link']?></td>
</tr>
<?php endforeach ?>
Run Code Online (Sandbox Code Playgroud)
使用for()循环来执行此操作会更容易,但要回答这个问题:
<?
$i = 0;
foreach ($section['Article'] AS $article):
if ($i == 3) { break; }
?>
...
<?
$i++;
endforeach
?>
Run Code Online (Sandbox Code Playgroud)
如果您的数组以数字方式编制索引,这将有所帮助
foreach($section['Article'] as $i => $article ):
if ($i > 3) break;
Run Code Online (Sandbox Code Playgroud)
否则 - 手动递增计数器:
$i = 0;
foreach($section['Article'] as $article ):
if ($i++ > 3) break;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
69361 次 |
| 最近记录: |