laravel 数组的递归演绎

2uluyalo  于 2023-02-10  发布在  其他
关注(0)|答案(1)|浏览(108)

我有这个数组的值

#    amount   deduction
[0]   50        10
[1]   160       20
[2]   300       20

我想对这些数组进行递归推导,直到数据为零,然后如果数组的第一个数量为零(0),则推导将添加到下一个数据,直到所有数量都为零(0)。下面的示例计算

[0]          [1]          [2]
50  - 10     160 - 20     300 - 20
40  - 10     140 - 20     280 - 20
30  - 10     120 - 20     260 - 20
20  - 10     100 - 20     240 - 20
10  - 10      80 - 20     220 - 20
 0            60 - 30     200 - 20
              30 - 30     180 - 20
              0           160 - 50
                          110 - 50
                           60 - 50
                           10 - 50
                           0

我不知道怎么做,但这是我的代码到目前为止。

foreach($data as $det)
{
   $_monthly_payment = $det->deduction;
   $_beginning_balance = $det->amount;
   $i = 0;

   do{
     $_new_balance = (($i + 1) == 1) ? $_beginning_balance : $_ending_balance;
     $_toward_principal      = $_monthly_payment;
     $_ending_balance        = $_new_balance - $_toward_principal;

     <tr>
        <td><?php echo number_format($_monthly_payment,2); ?></td>
        <td>$<?php echo number_format($_new_balance,2); ?> </td>
        <td>$<?php echo number_format($_toward_principal,2); ?> </td>
        <td>$<?php echo number_format($_ending_balance,2); ?> </td>
     </tr>
   }
   while( $i <= $_ending_balance );
}

别对我评头论足,我太需要这个了。谢谢你们。

已更新var_dump的结果

object(Illuminate\Support\Collection)#1334 (1) { 
    ["items":protected]=> array(3) { 
        [0]=> object(stdClass)#1341 (26) { 
            ["id"]=> int(130) ["account_name"]=> string(3) "QWE" ["amount"]=> float(50) ["deduction"]=> float(10) ["updated_at"]=> string(19) "2023-01-09 15:49:59" } 
        [1]=> object(stdClass)#1345 (26) { 
            ["id"]=> int(131) ["account_name"]=> string(3) "ABC" ["amount"]=> float(160) ["deduction"]=> float(20) ["updated_at"]=> string(19) "2023-01-09 15:49:59" } 
        [2]=> object(stdClass)#1326 (26) { 
            ["id"]=> int(132) ["account_name"]=> string(3) "XYZ" ["amount"]=> float(300) ["deduction"]=> float(20) ["updated_at"]=> string(19) "2023-01-09 15:49:59" } 
        } 
    }
b5buobof

b5buobof1#

这是@CBroe的答案大帮手,如果数组的第一个数据是零(0),继续推导。

<?php

$data = [
    ['amount' => 50, 'deduction' => 10],
    ['amount' => 160, 'deduction' => 20],
    ['amount' => 300, 'deduction' => 20],
];

$continue = true;

echo '<table>';
while($continue) {
    $continue = max(array_column($data, 'amount')) > 0;
    echo '<tr>';
    for($i=0; $i<count($data); ++$i) {
        echo '<td>';
        echo !isset($data[$i]['done']) ? max($data[$i]['amount'], 0) : '';
        if($data[$i]['amount'] <= 0 && !isset($data[$i]['done'])) {
            $data[$i]['done'] = true;
            if(isset($data[$i+1])) {
                $data[$i+1]['deduction'] += $data[$i]['deduction'];
            }
        }
        echo !isset($data[$i]['done']) ? ' - '.$data[$i]['deduction'] : '';
        echo '</td>';
        
        $data[$i]['amount'] -= $data[$i]['deduction'];
    }
    echo '</tr>';
    
} 
echo '</table>';

相关问题