php 如何将 foreach 循环限制为三个循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8765879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:27:32  来源:igfitidea点击:

how to limit foreach loop to three loops

php

提问by user1080247

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

回答by Ignacio Vazquez-Abrams

Slice the array.

切片数组。

foreach(array_slice($section['Article'], 0, 3) as $article ):

回答by Your Common Sense

first, prepare your data

首先,准备你的数据

$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;

then display it

然后显示它

<?php foreach($section['Article'] as $article ): ?>
<tr>
  <td><?php echo $article['title'] ?></td>
  <td>&nbsp;<?php echo $article['link']?></td>
</tr>
<?php endforeach ?>

回答by Fleep

It'd be easier to use a for() loop to do this, but to answer the question:

使用 for() 循环来做到这一点会更容易,但要回答这个问题:

<?
$i = 0;
foreach ($section['Article'] AS $article):
    if ($i == 3) { break; }
?>
...
<?
$i++;
endforeach
?>

回答by zerkms

This will help if your array is numerically indexed

如果您的数组是数字索引的,这将有所帮助

foreach($section['Article'] as $i => $article ):

    if ($i > 3) break;

Otherwise - manually increment the counter:

否则 - 手动增加计数器:

$i = 0;
foreach($section['Article'] as $article ):

    if ($i++ > 3) break;

回答by blake305

A foreach loop wouldn't be the best if you need to limit it. Try using a for loop.

如果您需要限制它,foreach 循环不是最好的。尝试使用 for 循环。

<?php

for(i=1; i<=3; i++)
{  
    $article = $section['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 } ?>

This code will make the text loop 3 times.

此代码将使文本循环 3 次。

回答by Ktheitroadala Indeep

Awesome one must try this one

很棒的人必须尝试这个

<?php $count = 0; $pages = get_pages('child_of=1119&sort_column=post_date&sort_order=desc');   foreach($pages as $page) {
$count++;
if ( $count < 50) {  // only process 10 ?>
 <div class="main_post_listing">  <a href="<?php echo get_page_link($page->ID) ?>"><?php echo $page->post_title ?></a><br /></div>
<?php
}  } ?>