开发者

problem with break while using it to break nested loops in php

When I execute this code:

$result = mysql_query("SELECT * FROM example_table");

$i = 0;
while ($row = mysql_fetch_array($result))
{
    for ($j = 0; $j < $c; $j++)
    {
        if ($db_array[$j]['ID'] == $row['id'])
        {
            $del_counter = 0;
            break 2;
        }
        else
        $del_counter = 1;
    }

    if ($del_counter == 1)
    {
    $del_array[$i] = $row['id'];
    $i++;
    }

}

This does not break the two level l开发者_如何转开发ooping. Instead, the $del_array stores all the row ids. I need to compare db_array[][id] with the array "row" (fetched from the database). And need to check which elements of db_array are deleted from the database. So, what I did is I tried to store the ids of deleted items in an array. But the break does not work. Am I missing something?

Thanks in anticipation.

BG


According to the PHP manual:

1) break ends execution of the current for, foreach, while, do-while or switch structure
2) break accepts an optional numeric argument which tells it 
how many (the above mentioned) nested enclosing structures are to be broken out of

Your break is at the level 2, and hence break 2; should work as expected, so the problem is elsewhere. Is the break 2 executed at all?

In any case, I would recommend a more robust solution, e.g.

$ok = True;
while (($row = mysql_fetch_array($result)) && $ok) {
      ...
      if (...) $ok = False;  // anywhere deep
      ...
      if ($ok) {...}
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜