php - 嵌套循环,打破内循环并继续主循环

时间:2015-09-09 17:11:01

标签: php loops nested break continue

我有以下循环,当内循环内的检查满足条件时,我想Source while循环。我找到了一个解决方案here我在以下示例中应用了),但它适用于Image

x:Name

当内部循环已经<DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding CPAName}" /> <Image x:Name="image" Source="/Images/TimelineIconGreenTransparent.gif" Margin="5,0,0,0" Height="15" Width="15" Visibility="{Binding StaticInd, Converter={StaticResource BoolVisConv}, ConverterParameter=inverse, Mode=OneWay}"/> </StackPanel> <DataTemplate.Triggers> <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="True"> <Setter TargetName="image" Property="Source" Value="/Images/Whateveryourotherimageisnamed.gif" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> 时,PHP是否有自己的continue主循环方式?

2 个答案:

答案 0 :(得分:15)

在阅读了对此问题的评论(被其作者删除)并进行了一些研究之后,我发现continue还有break的参数,就像{{1} }。我们可以像continue那样添加号码:

while($something) {

   foreach($array as $value) {
    if($ok) {
       continue 2;
       // continue the while loop
    }

       foreach($value as $val) {
          if($ok) {
          continue 3;
          // continue the while loop
          }
       }
   }
}

答案 1 :(得分:0)

我认为,在你处理完整的内部foreach之前,你没有运行continue,这是无关紧要的。您需要在原地执行continue,而不是等到循环结束。

将代码更改为此

while($something) {

    foreach($array as $value) {
        if($ok) {
            continue;    // start next foreach($array as $value)
        }

        foreach($value as $val) {
            if($ok) {
               break 2;    // terminate this loop and start next foreach($array as $value)
            }
        }
    }

}

RE:您的评论

while($something) {

    if($somevalue) {
        // stop this iteration
        // and start again at iteration + 1
        continue;    
    }


}