循环重复两次

Loops repeats twice

$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

花了几个小时,但无法弄清楚为什么循环运行多次,甚至是内部循环!我一定是在做一些非常愚蠢的事情,只是找不到内容和位置。

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

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

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

同样适用于 $j。

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