两次迭代后打破foreach循环 [英] Breaking a foreach loop after two iterations

查看:142
本文介绍了两次迭代后打破foreach循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  foreach($ posttags as $ tags)如何在两次迭代之后制止foreach循环,如下所示: {
$ tag1 = $ tags-> slug;
}这是第一次迭代,存储在变量$ tag1

foreach($ posttags as $ tags){
$ tag2 = $ tags-> slug ;
} ----这是第二次迭代,存储在变量$ tag2

brake中; ---制动foreach循环

有什么想法?

解决方案

最明显的答案是不使用foreach循环,如果你不想循环每个项目。使用其他循环结构,或者手动抓取第一个和第二个项目,如果这是你真正想要的。


How can I brake a foreach loop after two iterations, something like this:

foreach($posttags as $tags){     
  $tag1 = $tags->slug;  
} --- this is the first iteration, stored in the variable $tag1

foreach($posttags as $tags){     
  $tag2 = $tags->slug;  
} ---- this is the second iteration, stored in the variable $tag2

brake; --- brake the foreach loop

Any ideas?

解决方案

The most obvious answer is not to use a foreach loop if you don't want to loop through each item. Use some other looping structure, or grab the first and second items manually if that's what you really want.

这篇关于两次迭代后打破foreach循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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