PHP negatives keep adding

前端 未结 3 863
予麋鹿
予麋鹿 2021-01-29 16:48

I have this code here...

$remaining = 0;
foreach($clientArrayInvoice as $key=>$row){
            $remaining = $remaining + $row[\'total\'];   
}
3条回答
  •  执念已碎
    2021-01-29 17:24

    I am not sure what your question is exactly, but this would keep adding the absolute values if $remaining is negative until it is positive again.

    $remaining = $remaining + ($remaining < 0 && $row['remainingbalance'] < 0 ? -1 : 1) * $row['remainingbalance']);

    This works for your example, it would be 0 - 51.75 + 17.85 = -33.9. But I am not sure if it's the behavior you want in the bigger picture.

提交回复
热议问题