每次迭代后如何减少总数

时间:2019-03-30 13:35:11

标签: php

我有一个$ paymentsTotal变量,它是所有付款的总和。然后,我要遍历所有未清金额,并减少每个未清金额的$ paymentsTotal变量。

例如:

TotalPayments = 900 - 200
                700 - 300
                400 - 100
                300 - 300
Stop             0

并且只要$ paymentsTotal大于未付金额,它就应该返回true。

我尝试过:

foreach ($invoices as $invoice) {
  if($paymentTotal >= $paymentTotal -= $invoice->amount) {
        echo $invoice->amount . ' - PAID <br>';
   }
}

但是无论如何它都会回显每个结果。

1 个答案:

答案 0 :(得分:1)

尝试类似...

$paymentTotal = 0; // whatever this is before iteration

foreach($invoices as $invoice) {
    if ($invoice->amount > $paymentTotal) {
        $paymentTotal -= $invoice->amount; // assuming you have a field for the paid amount
        $invoice->amount = $paymentTotal; // new invoice amount
    echo "{$invoice->amount} - PAID <br>";
    }
}