在两次迭代后中断foreach循环


Breaking a foreach loop after two iterations

如何在两次迭代后中断foreach循环,如下所示:

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

任何想法?

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