在 PHP 中结合 foreach 和 while
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18833934/
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
Combining foreach and while in PHP
提问by Michael.Lumley
This seems like it should be a simple question, but I can't find a good answer. Is there a way of putting a condition on a foreach loop? I'd like something like this:
这似乎应该是一个简单的问题,但我找不到好的答案。有没有办法在 foreach 循环上放置条件?我想要这样的东西:
foreach ($array as $value WHILE $condition == true)
{ //do some code }
Of course I could just put an if condition inside of the foreach loop as follows:
当然,我可以在 foreach 循环中放置一个 if 条件,如下所示:
foreach ($array as $value)
{
if($condition == true)
{//do some code}
}
The only thing is that I'd like to stop iterating over the array once the if condition becomes false, for the purpose of improving performance. No need to run through the remainder of the foreach loop to determine that $condition is false once it becomes false.
唯一的事情是,一旦 if 条件变为假,我想停止对数组进行迭代,以提高性能。无需遍历 foreach 循环的其余部分来确定 $condition 一旦变为 false 就为 false。
Any suggestions? Am I misisng something obvious?
有什么建议?我错过了一些明显的东西吗?
回答by nice ass
回答by legrandviking
You can easily use the break keyword to exit a foreach loop at the exact moment you wish. this is the simplest way of doing this i can think of at the moment.
您可以轻松地使用 break 关键字在您希望的确切时刻退出 foreach 循环。这是我目前能想到的最简单的方法。
foreach ($array as $value)
{
if($condition == true)
{
//do some code
break;
}
}
回答by Jay Sheth
You could also try a regular for loop, which has a condition built-in. The only thing is that you'll have to access the element of the array using its index.
您还可以尝试使用内置条件的常规 for 循环。唯一的问题是您必须使用其索引访问数组的元素。
<?php
//Basic example of for loop
$fruits = array('apples', 'figs', 'bananas');
for( $i = 0; $i < count($fruits); $i++ ){
$fruit = $fruits[$i];
echo $fruit . "\n";
}
This is a slightly more complicated example, that stops executing as soon as it finds a fig.
这是一个稍微复杂一些的例子,它在找到 fig 后立即停止执行。
<?php
//Added condition to for loop
$fruits = array('apple', 'fig', 'banana');
$continue = true;
for( $i = 0; $i < count($fruits) && $continue == true; $i++ ){
$fruit = $fruits[$i];
if( $fruit == 'fig' ){
$continue = false;
}
echo $fruit . "\n";
}
I hope that helps.
我希望这有帮助。
回答by Jay Sheth
foreach ($array as $value) {
if($condition) {
//do some code
}
else {
break;
}
}
回答by Federico
maybe you can use the break; sentence
也许你可以利用休息时间;句子
foreach ($array as $value) { if($condition == true) {//do some code} else { break; } }
foreach ($array as $value) { if($condition == true) {//做一些代码} else { break; } }
回答by Whizou
An alternative whitout using foreach but only while. Like this, the loop will end when the condition return false.
使用 foreach 但只有 while 的另一种选择。像这样,当条件返回 false 时循环将结束。
$i = 0;
while (condition(array[$i]) === true) {
$continue = true;
echo array[i];
$i++;
}