php 在 3 次迭代后如何停止这个 foreach 循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2865373/
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
How would I stop this foreach loop after 3 iterations?
提问by Tapha
Here is the loop.
这是循环。
foreach($results->results as $result){
echo '<div id="twitter_status">';
echo '<img src="'.$result->profile_image_url.'" class="twitter_image">';
$text_n = $result->text;
echo "<div id='text_twit'>".$text_n."</div>";
echo '<div id="twitter_small">';
echo "<span id='link_user'".'<a href="http://www.twitter.com/'.$result->from_user.'">'.$result->from_user.'</a></span>';
$date = $result->created_at;
$dateFormat = new DateIntervalFormat();
$time = strtotime($result->created_at);
echo "<div class='time'>";
print sprintf('Submitted %s ago', $dateFormat->getInterval($time));
echo '</div>';
echo "</div>";
echo "</div>";
回答by pakore
With the breakcommand.
随着break命令。
You are missing a bracket though.
虽然你缺少一个括号。
$i=0;
foreach($results->results as $result){
//whatever you want to do here
$i++;
if($i==3) break;
}
More info about the breakcommand at: http://php.net/manual/en/control-structures.break.php
有关该break命令的更多信息:http: //php.net/manual/en/control-structures.break.php
Update: As Kylepointed out, if you want to break the loop it's better to use forrather than foreach. Basically you have more control of the flow and you gain readability. Note that you can only do this if the elements in the array are contiguous and indexable(as Colonel Sponsz pointed out)
更新:正如凯尔所指出的,如果你想打破循环,最好使用for而不是foreach. 基本上,您可以更好地控制流程并获得可读性。请注意,只有在数组中的元素是连续且可索引的情况下才能执行此操作(正如Sponsz 上校指出的那样)
The code would be:
代码将是:
for($i=0;$i<3;$i++){
$result = $results->results[i];
//whatever you want to do here
}
It's cleaner, it's more bug-proof (the control variables are all inside the forstatement), and just reading it you know how many times it will be executed. break/ continueshould be avoided if possible.
它更干净,更能防错(控制变量都在for语句中),只要阅读它你就知道它会被执行多少次。break/continue应尽可能避免。
回答by polygenelubricants
- Declare a variable before the loop, initialize to 0.
- Increment variable at the beginning of the body of for-each.
- Check variable at the end of the body of for-each.
- If it's 3,
break.
- If it's 3,
- 在循环之前声明一个变量,初始化为0。
- 在 for-each 主体的开头增加变量。
- 检查 for-each 主体末尾的变量。
- 如果是3
break。
- 如果是3
You have to be careful with this technique because there could be other break/continuein the for-each body, but in your case there isn't, so this would work.
您必须小心使用此技术,因为for-each 主体中可能有其他break/ continue,但在您的情况下没有,所以这会起作用。
回答by Chris Schmich
Increment some counter $iat the beggining of the loop and break;when it reaches 3, e.g.:
$i在循环开始时增加一些计数器,break;当它达到 3 时,例如:
if ($i++ == 3)
break;
回答by Mark Baker
foreach($results->results as $i => $result){
if($i==3) break;
//whatever you want to do here
}

