将此PHP foreach语句限制为仅5个循环

时间:2017-07-06 09:02:43

标签: php foreach

我怎么能将这个foraech语句限制为5个循环?我想我应该使用Break,但我不知道该把它放在哪里。

 <?php if(!empty($locations)): foreach($locations as $location):  ?>
<?php if(empty($location["title"])) continue; ?>
<li>
    <a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
        <?php esc_html_e($location["title"]) ?>
    </a>
    <?php if($param->count): ?>
    <div class="wpjb-widget-item-count">
        <div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
    </div>
    <?php endif; ?>
</li>
<?php endforeach; ?>

6 个答案:

答案 0 :(得分:1)

有三种方法:

方法1:foreach with counter var

ddply(.data = df,.variables = "User",.fun = sol)

#    User     Time  Flag Time_Difference
#1     A  11:39:30    1               0
#2     A  11:37:53    1               0
#3     A  20:44:19    1               0
#4     A  22:58:42    2            8063
#5     A  23:01:54    1             192
#6     B  23:03:00    1               0
#7     B  23:03:33    1               0
#8     B  23:03:53    1               0
#9     B  15:00:42    3           28991
#10    B  19:35:31    2           16489
#11    B  19:35:34    1               3
#12    C  10:19:06    1               0
#13    C  10:59:50    1               0 
#14    C  10:59:50    1               0
#15    C  12:16:36    1               0
#16    C  12:16:36    1               0

方法2:foreach使用$ key =&gt; $ val

$counter = 1;

foreach($locations as $location) {
    // use $location here

    if($counter++ == 5) {
        break;
    }

}

方法3:for循环

foreach($locations as $key=>$val) {

    // Your content goes here
    if($key === 4) {
        break;
    }
}

答案 1 :(得分:1)

您可以先使用array_slice()来获取一个不超过5个元素的新数组。

$locations = array_slice($locations, 0, 5);

然后一切都没改变。

答案 2 :(得分:0)

在循环中使用计数器变量,您可以控制/限制任何数字。

示例:

$counter = 0;
foreach($locations as $location):
    if($counter++ == 5): 
        break;
    // Your other content goes here
    endif;
endforeach;

答案 3 :(得分:0)

添加一个变量...每次迭代递增...在达到5之后才开始循环。

<?php $i = 1;?>         

<?php if(!empty($locations)): foreach($locations as $location):  ?>
    <?php if(empty($location["title"])) continue; ?>
    <li>
        <a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
            <?php esc_html_e($location["title"]) ?>
        </a>
        <?php if($param->count): ?>
        <div class="wpjb-widget-item-count">
            <div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
        </div>
        <?php endif; ?>
    </li>
    <?php  if ($i++ == 5) break; ?>
    <?php endforeach; ?>

答案 4 :(得分:0)

您可以使用for():

<?php if(!empty($locations)): 
for($i=0; $i<5; $i++) {
    $location = $locations[$i];
    <?php if(empty($locations["title"])) continue; ?>
        <li>
            <a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
                <?php esc_html_e($location["title"]) ?>
            </a>
            <?php if($param->count): ?>
            <div class="wpjb-widget-item-count">
                <div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
            </div>
            <?php endif; ?>
        </li>
}

答案 5 :(得分:0)

<?php if(!empty($locations)): foreach($locations as $key => $location):  ?>
  <?php if(empty($location["title"])) continue; ?>
  <?php if($key==5) break;?>
  <li>
    <a href="<?php esc_attr_e($url.$glue.http_build_query($location["query"])) ?>">
    <?php esc_html_e($location["title"]) ?>
    </a>
    <?php if($param->count): ?>
    <div class="wpjb-widget-item-count">
    <div class="wpjb-widget-item-num"><?php echo intval($location["count"]) ?></div>
    </div>
    <?php endif; ?>
  </li>
<?php endforeach; ?>