0

我正在寻找专家来解释为什么 FOREACH 在更新和中断后没有停止?

代码在 3x10 矩阵中找到位置,当它找到它并更新它假设中断并退出但它继续循环的表时......非常非常奇怪。

checkPosUnder('x',array(100000));

function checkPosUnder($userToFill,$underUsers) {
    echo "<br>enter func Check on all this users:<br>";
    print_r($underUsers);
    $newGenUsers = array();
    foreach($underUsers as $curuser) {
        echo "<br>NEW LOOP<br>";
        //$underThisUser
        $freeq = $db->prepare("SELECT * FROM tueeunet WHERE username=?");
        $freeq->execute(array($curuser));
        $freeq->setFetchMode(PDO::FETCH_ASSOC);
        $free = $freeq->fetch();

        if($free['under1']=='') {
            $updateQuery = $db->prepare("UPDATE tueeunet SET under1=? WHERE username=? ");
            $updateQuery->execute(array($userToFill,$curuser));
            echo "1<br>";
            break;
        }
        elseif($free['under2']=='') {
            $updateQuery = $db->prepare("UPDATE tueeunet SET under2=? WHERE username=? ");
            $updateQuery->execute(array($userToFill,$curuser)); 
            echo "2<br>";
            break;
        }
        elseif($free['under3']=='') {
            $updateQuery = $db->prepare("UPDATE tueeunet SET under3=? WHERE username=? ");
            $updateQuery->execute(array($userToFill,$curuser));
            echo "3<br>";
            break;
        }
        else {
            array_push($newGenUsers, $free['under1'],$free['under2'],$free['under3']);
            echo "<br>";
            print_r($newGenUsers);
            echo "<br>";
            checkPosUnder($userToFill,$newGenUsers);
        }
    }
}
4

3 回答 3

0

你可以为它使用一个标志..

    foreach($underUsers as $curuser) {
    $updated = false;
            if($free['under1']=='') {
                $updateQuery = $db->prepare("UPDATE tueeunet SET under1=? WHERE username=? ");
                $updateQuery->execute(array($userToFill,$curuser));
                echo "1<br>";
                $updated = true;
            }

    if($updated)
    Break;
}//Foreach 
于 2013-07-05T13:01:46.057 回答
0

你可以试试

break(2);

手册没有特别提到 IF,但它是一个封闭符 { } 可能break只是退出 IF 而不是 foreach。

于 2013-07-05T10:38:38.167 回答
0

可能您的代码总是else部分输入。

或者,您可以在解决方案中使用标志

checkPosUnder('x',array(100000));

function checkPosUnder($userToFill,$underUsers) {
    echo "<br>enter func Check on all this users:<br>";
    print_r($underUsers);
    $newGenUsers = array();
    foreach($underUsers as $curuser) {
        $Break = false;
        echo "<br>NEW LOOP<br>";
        //$underThisUser
        $freeq = $db->prepare("SELECT * FROM tueeunet WHERE username=?");
        $freeq->execute(array($curuser));
        $freeq->setFetchMode(PDO::FETCH_ASSOC);
        $free = $freeq->fetch();

        if($free['under1']=='') {
            $updateQuery = $db->prepare("UPDATE tueeunet SET under1=? WHERE username=? ");
            $updateQuery->execute(array($userToFill,$curuser));
            echo "1<br>";
            $Break = true;
            break;
        }
        elseif($free['under2']=='') {
            $updateQuery = $db->prepare("UPDATE tueeunet SET under2=? WHERE username=? ");
            $updateQuery->execute(array($userToFill,$curuser)); 
            echo "2<br>";
            $Break = true;
            break;
        }
        elseif($free['under3']=='') {
            $updateQuery = $db->prepare("UPDATE tueeunet SET under3=? WHERE username=? ");
            $updateQuery->execute(array($userToFill,$curuser));
            echo "3<br>";
            $Break = true;
            break;
        }
        else {
            array_push($newGenUsers, $free['under1'],$free['under2'],$free['under3']);
            echo "<br>";
            print_r($newGenUsers);
            echo "<br>";
            checkPosUnder($userToFill,$newGenUsers);
        }

        if($Break) { Break; }
    }
}
于 2013-07-05T13:50:24.790 回答