循环重复两次

时间:2015-02-21 20:24:36

标签: php loops do-while

$n = 0;
do {
    echo "START PAGE\n";
    echo "total pages: " . $response->PaginationResult->TotalNumberOfPages . "\n";
    echo "current page: " . $currentPage . "\n";
    echo "iteration: " . $n;
    if ($n == 2) {
        break;
    }

    $j = 0;
    foreach($response->SaleRecord as $SaleRecord) {

        if ($j == 2) {
            break;
        }

        echo "\n\tSTART RECORD";

        foreach($SaleRecord->SellingManagerSoldTransaction as $transaction) {
            echo "\n\t\tRECORD INNER";
            foreach($getOrders->OrderArray->Order as $orders) {
                echo "\n\t\t\tGETORDERS - " . $itemID;
                foreach($orders->TransactionArray->Transaction as $transaction) {
                    echo "\n\t\t\t\tTXN ARRAY - " . $itemID;
                }
            }
        }

        $j++;
        echo "\n\tEND RECORD";
    }

    $n++;
    $currentPage+= 1;
    echo "\nEND PAGE\n\n\n";
}

while (isset($response->SaleRecord) && $currentPage <= $response->PaginationResult->TotalNumberOfPages);

输出:

START PAGE
total pages: 231
current page: 1
iteration: 0
    START RECORD
        RECORD INNER
            GETORDERS - 997
                TXN ARRAY - 997
    END RECORD
    START RECORD
        RECORD INNER
            GETORDERS - 998
                TXN ARRAY - 998
            GETORDERS - 998
                TXN ARRAY - 998
    END RECORD
END PAGE


START PAGE
total pages: 231
current page: 2
iteration: 1
    START RECORD
        RECORD INNER
            GETORDERS - 997
                TXN ARRAY - 997
            GETORDERS - 997
                TXN ARRAY - 997
    END RECORD
    START RECORD
        RECORD INNER
            GETORDERS - 998
                TXN ARRAY - 998
            GETORDERS - 998
                TXN ARRAY - 998
    END RECORD
END PAGE

START PAGE
total pages: 231
current page: 3
iteration: 2

花了很多时间,但无法弄清楚为什么循环会多次运行,甚至是内循环!我必须做一些非常愚蠢的事情,只是无法找到什么和在哪里。

2 个答案:

答案 0 :(得分:0)

当n等于2时,他断开。你从n = 0(第一个循环)开始,然后变为n = 1(第二个循环)然后当n == 2(第三个循环)时它会中断。您应该将中断块更改为以下内容然后它可以工作:

if ($n == 1) {
    break;
}

答案 1 :(得分:0)

如果$ n = 0并且您的中断条件是$ n == 2,那就是2次迭代:$ n = 0且$ n = 1

$ j也一样。

将您的条件更改为$ n == 1和$ j == 1(或$ n&lt; 2和$ j&lt; 2)