在PHP

时间:2015-09-18 07:06:21

标签: php for-loop while-loop

我需要帮助减去支付给每月摊还的总金额,

例如,用户支付了13000,他的每月摊还额仅为5000,我想像以下一样进行循环

  • 5000
  • 5000
  • 3000

更新

$ total_payment = 13000; $ mi = 5000;

for ($x = 0; $mi <= $total_payment; $x++) {                 
    echo $total = $total_payment - $sales_mi, "<br/>";              
}

2 个答案:

答案 0 :(得分:3)

可能会有所帮助 -

$total_payment = 13000;
$sales_mi = 5000;
$count = ceil($total_payment / $sales_mi);


$new = [];
for ($x = 1; $x <= $count; $x++) {
   $total_payment -= $sales_mi;      
   if($total_payment >= 0) {
      $new[] = $sales_mi;
   } else {
      $new[] = $total_payment + $sales_mi;
      break;
   }
}

var_dump($new);

<强>输出

array(3) {
  [0]=>
  int(5000)
  [1]=>
  int(5000)
  [2]=>
  int(3000)
}

您可以根据需要使用数组。

答案 1 :(得分:0)

$amort = 5000;
$remaining=15000;

for($remaining += $amort; $remaining > $amort; print((($remaining-=$amort) < $amort ? $remaining : $amort) . "<br>"));