【问题标题】:Breaking out of nested loops in PHP打破 PHP 中的嵌套循环
【发布时间】:2012-10-12 07:57:38
【问题描述】:

我有一个嵌套循环,即

while()
{
    for()
    {

    }
}

在我的页面中,我试图打破 for 循环并进入 while 循环而不退出它。我试过break;break 1;goto xxx;,但这些都不起作用。这是代码:

while( $results = pg_fetch_assoc( $result ) )
{

    $url = $results['url'];
    for( $i = 0; $i < ($recordnum - 1); $i++ )
    {

        $urlpopup = $foldername . "/" . $namecomingform[$i];

        if( $url == $urlpopup )
        {

            $isthererecord = 'yes';

            goto getoutoffor;
        }
        else
        {

            $isthererecord = 'no';
        }
    }//end of for
    getoutoffor:

    if( $isthererecord == 'no' )
    {

        $sql = "UPDATE pictures SET erased='1' WHERE url='" . $url . "'";

        $run = pg_query( $sqlconnection, $sql );
    }
} //end of while

它似乎只检查数据库中的一条记录。

感谢任何帮助。

【问题讨论】:

  • break 应该可以正常工作。你确定不是吗?
  • 您是否检查过( $url == $urlpopup ) 的比较是否确实按预期工作?
  • 您的 for 循环依赖于 $recordnum,它似乎没有在任何地方初始化。
  • 您可以使用布尔变量而不是将字符串设置为“是”或“否”。
  • 这是我的代码的一部分,我实际上是在这部分代码的上面初始化它。

标签: php for-loop while-loop break nested-loops


【解决方案1】:

你可能关心这部分:

$url = $results['url'];
for( $i = 0; $i < ($recordnum - 1); $i++ )
{

    $urlpopup = $foldername . "/" . $namecomingform[$i];

    if( $url == $urlpopup )
    {

        $isthererecord = 'yes';

        goto getoutoffor;
    }
    else
    {

        $isthererecord = 'no';
    }
}//end of for
getoutoffor:

阅读。您在这里循环只是为了测试$url 末尾的数字是否在0$recordnum - 1 之间。而是从字符串中提取前缀和数字。比较前缀,如果匹配,检查数字是否在该范围内。

这样做还可以进一步了解您在代码中遇到的逻辑问题。从您问题的措辞看来,您在这里找错地方了,所以我希望我的回答对您有所帮助。

【讨论】:

  • " 你在这里循环只是为了测试 $url 末尾的数字在 0 和 $recordnum - 1 之间。" - 你是怎么得出这个结论的?
  • $isthererecord 是结果,循环仅针对单个 if 比较完成。 $i 表示一个哈希图,即 范围
【解决方案2】:

我知道我来晚了,但我遇到了类似的问题。应用到您的情况,我要做的是将代码更改为以下内容:

$LoopBreak = 'no';
while( $results = pg_fetch_assoc( $result ) )
{
if ($LoopBreak == 'yes') {
break;//Insert break statement here
}

else {    //This is First else

$url = $results['url'];
for( $i = 0; $i < ($recordnum - 1); $i++ )
{
    $urlpopup = $foldername . "/" . $namecomingform[$i];

    if( $url == $urlpopup )
    {
        $isthererecord = 'yes';
        $LoopBreak = 'yes'; //Variable added in place of break here
    }
    else
    {
        $isthererecord = 'no';
    }
}//end of for

if( $isthererecord == 'no' )
{
    $sql = "UPDATE pictures SET erased='1' WHERE url='" . $url . "'";
    $run = pg_query( $sqlconnection, $sql );
}

}//End First else
} //end of while

【讨论】:

    【解决方案3】:

    正如其他人所说,break; 是您应该使用的。如果您的代码不能与 break; 一起使用,那么很可能是其他问题,例如 if 语句检查或 for 循环中的条件从未被满足(这意味着 for 循环甚至永远不会启动) .你真的应该更多地调试你的代码。 但不要使用goto。使用break;

    假设所有变量都正确,下面的代码应该真的可以工作:

    while( $results = pg_fetch_assoc( $result ) )
    {
        $url = $results['url'];
        for( $i = 0; $i < ($recordnum - 1); $i++ )
        {
            $urlpopup = $foldername . "/" . $namecomingform[$i];
    
            if( $url == $urlpopup )
            {
                $isthererecord = 'yes';
                break;
            }
            else
            {
                $isthererecord = 'no';
            }
        }//end of for
    
        if( $isthererecord == 'no' )
        {
            $sql = "UPDATE pictures SET erased='1' WHERE url='" . $url . "'";
            $run = pg_query( $sqlconnection, $sql );
        }
    } //end of while
    

    【讨论】:

      【解决方案4】:

      这不是处理这个问题的最佳方式,但您可以在 FOR 循环中添加一个额外条件 (&& $isthererecord != 'yes') 并初始化 $isthererecord 进入循环之前的变量,所以你修改的代码最终会像下面这样:

      $isthererecord = '';
      while ( $results = pg_fetch_assoc( $result ) ) {
          $url = $sorgusonucu['url'];
          for ( $i = 0; $i < ( $recordnum - 1 ) && $isthererecord != 'yes'; $i++ ) {
              $urlpopup = $foldername . "/" . $namecomingform[ $i ];
      
              if ( $url == $urlpopup ) {
                  $isthererecord = 'yes';
                  continue;
              }
              else {
                  $isthererecord = 'no';
              }
          }
      
          if ( $isthererecord == 'no' ) {
              $sql = "UPDATE pictures SET erased = '1' WHERE url = '" . $url . "'";
              $run = pg_query( $sqlconnection, $sql );
          }
      }
      

      【讨论】:

      • 这仍然不起作用。它只检查来自数据库的一条记录。
      • 然后,正如其他人指出的那样,您的 if ( $url == $urlpopup ) 条件有问题...这些变量永远不会相等,您应该进一步调试您的代码
      • 即使比较不正确,它也应该通过 while 循环,因为我的数据库中有不止一条记录。我正在打印来自数据库和 for 循环中的数组的两条记录。我在 while 循环中只打印了一条记录。
      【解决方案5】:

      for 循环中永远不需要 else

      while ( $results = pg_fetch_assoc( $result ) ) {
          $url = $sorgusonucu['url'];
          for ( $i = 0; $i < ( $recordnum - 1 ) && $isthererecord != 'yes'; $i++ ) {
              $urlpopup = $foldername . "/" . $namecomingform[ $i ];
      
              if ( $url === $urlpopup ) {
                  $sql = "UPDATE pictures SET erased = '1' WHERE url = '" . $url . "';";
                  $run = pg_query( $sqlconnection, $sql );
                  break;
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2012-01-19
        • 1970-01-01
        • 2016-09-30
        • 2015-12-05
        • 2012-03-30
        • 2010-10-13
        相关资源
        最近更新 更多