如何按多维数组中的相同值分组并计算总和

时间:2019-08-22 02:10:08

标签: php foreach

我正在尝试创建一个简单的报告,该报告将显示每月赚多少钱。我想按月分组并计算该月的总和。

这是数组的样子:

$payouts = Array
(
    [0] => Stripe\Payout Object
        (
            [id] => po_1F8dyGDi2QUa2pDzir2SvvEJ
            [object] => payout
            [amount] => 11291
            [arrival_date] => 1566172800
            [automatic] => 1
            [balance_transaction] => txn_1F8dyGDi2QUa2pDzYSkN2Kq4
            [created] => 1566094696
            [currency] => cad
            [description] => STRIPE PAYOUT
            [destination] => ba_1EzNmkDi2QUa2pDzApl8u4hs
            [failure_balance_transaction] => 
            [failure_code] => 
            [failure_message] => 
            [livemode] => 
            [metadata] => Stripe\StripeObject Object
                (
                )

            [method] => standard
            [source_type] => card
            [statement_descriptor] => 
            [status] => paid
            [type] => bank_account
        )

    [1] => Stripe\Payout Object
        (
            [id] => po_1F8HDoDi2QUa2pDz9BSKsIv2
            [object] => payout
            [amount] => 11339
            [arrival_date] => 1566172800
            [automatic] => 1
            [balance_transaction] => txn_1F8HDoDi2QUa2pDz3s2iAaZ4
            [created] => 1566007248
            [currency] => cad
            [description] => STRIPE PAYOUT
            [destination] => ba_1EzNmkDi2QUa2pDzApl8u4hs
            [failure_balance_transaction] => 
            [failure_code] => 
            [failure_message] => 
            [livemode] => 
            [metadata] => Stripe\StripeObject Object
                (
                )

            [method] => standard
            [source_type] => card
            [statement_descriptor] => 
            [status] => paid
            [type] => bank_account
        )

    [2] => Stripe\Payout Object
        (
            [id] => po_1F26tNDi2QUa2pDzVuHCdFie
            [object] => payout
            [amount] => 52344
            [arrival_date] => 1564617600
            [automatic] => 1
            [balance_transaction] => txn_1F26tNDi2QUa2pDz4wKioVNf
            [created] => 1564537573
            [currency] => cad
            [description] => STRIPE PAYOUT
            [destination] => ba_1EzNmkDi2QUa2pDzApl8u4hs
            [failure_balance_transaction] => 
            [failure_code] => 
            [failure_message] => 
            [livemode] => 
            [metadata] => Stripe\StripeObject Object
                (
                )

            [method] => standard
            [source_type] => card
            [statement_descriptor] => 
            [status] => paid
            [type] => bank_account
        )

    [3] => Stripe\Payout Object
        (
            [id] => po_1EwJllDi2QUa2pDzKPpXfMe6
            [object] => payout
            [amount] => 22366
            [arrival_date] => 1563235200
            [automatic] => 1
            [balance_transaction] => txn_1EwJllDi2QUa2pDzXM0S3sBv
            [created] => 1563157105
            [currency] => cad
            [description] => STRIPE PAYOUT
            [destination] => ba_1EvDDqDi2QUa2pDzpdVr5PLg
            [failure_balance_transaction] => 
            [failure_code] => 
            [failure_message] => 
            [livemode] => 
            [metadata] => Stripe\StripeObject Object
                (
                )

            [method] => standard
            [source_type] => card
            [statement_descriptor] => 
            [status] => paid
            [type] => bank_account
        )

)

这是我所拥有的代码,它将以人类可读的方式显示月份和金额。

   <table>
            <thead> 
                                 <tr>
                                    <th>Date Created</th>
                                     <th>Arrival Date</th>
                                      <th>Month</th>
                                       <th>Year</th>
                                  <th>Amount</th>

                                 </tr>
                                 </thead>
                                 <tbody>

                                 <?php 
                                 $totalRevenueBE = 0;
                                 $convertedRBE = 0;
                                 foreach ($payouts->data as $payout) {

                                     $amount_gross = $payout->amount;
                                     $stripe_fee = $payout->fee;
                                     $amount_net = $amount_gross -$stripe_fee;

                                      //convert amount_net to dollars : $1 = 100cents
                                    $convertedAmount = $amount_net / 100;

                                     //calculate total revenue BE
                                    $total = $amount_net;
                                    $totalRevenueBE += $total;  

                                      //convert revenueBE_total to dollars : $1 = 100cents
                                    $convertedRBE = $totalRevenueBE / 100;

                                    //convert unix timestamp to date
                                    $date_created=$payout->created;
                                    $date_available=$payout->arrival_date;
                                    $d_created = date('Y-m-d H:i:s', $date_created);
                                    $d_available = date('Y-m-d H:i:s', $date_available);

                                     ?>
                                        <tr>
                                            <td><?=$d_created?></td>
                                             <td><?=$d_available?></td>
                                                 <td><?=date('F',strtotime($d_created))?></td>
                                                  <td><?=date('Y',strtotime($d_created))?></td>
                                         <td><?=$convertedAmount?></td>

                                     </tr>
                                    <?php }?>
                                 </tbody>


        </table>

上面的代码将显示以下内容:

Date Created        | Arrival Date        | Month | Year | Amount
2019-08-18 04:18:16 |2019-08-19 02:00:00  | August| 2019 | 112.91
2019-08-17 04:00:48 |2019-08-19 02:00:00  | August| 2019 | 113.39
2019-07-31 03:46:13 |2019-08-01 02:00:00  | July  | 2019 | 523.44
2019-07-15 04:18:25 |2019-07-16 02:00:00  | July  | 2019 | 223.66

但这是我想要的结果。我想按月分组并计算总和。

 Month | Year | Amount
 August| 2019 | 226.3
 July  | 2019 | 747.1

1 个答案:

答案 0 :(得分:0)

move desire data to another array  and use that for display 
 <?php
$data_array=[];  
$month= date('F',strtotime($d_created));
$year=date('Y',strtotime($d_created));

$sum=isset($data_array[$year][$month]['amount'])?$data_array[$year][$month]['amount']:0;
$data_array[$year][$month]['month']=$month;
$data_array[$year][$month]['year']=$year;
$data_array[$year][$month]['amount']=$sum+$convertedAmount;

 echo "<pre>";print_r($data_array); ?

> Blockquote