Php 如何在多维数组中按相同值分组并计算和
我试图创建一个简单的报告,显示每月的收入。我想按月分组,计算那个月的总数 这就是阵列的外观:Php 如何在多维数组中按相同值分组并计算和,php,foreach,Php,Foreach,我试图创建一个简单的报告,显示每月的收入。我想按月分组,计算那个月的总数 这就是阵列的外观: $payouts = Array ( [0] => Stripe\Payout Object ( [id] => po_1F8dyGDi2QUa2pDzir2SvvEJ [object] => payout [amount] => 11291 [arrival_
$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>
但这是我想要的结果。我想按月分组并计算总数
Month | Year | Amount
August| 2019 | 226.3
July | 2019 | 747.1
将所需数据移动到另一个数组并使用该数组进行显示
Hi@Random,您可以仅通过使用Mqsql查询获得所需结果,“但这是我想要的结果。”-您的尝试是在哪里获得的?它的具体问题在哪里?请注意,这不是一个代码编写服务,您应该首先表现出合理的努力。非常感谢。这就是答案。我想知道,是否有更动态的方法来访问数组中的值?现在我做的是foreach($n_数组作为$n_事务){echo$n_事务['August']['amount'];}
但是如果我有12个月,这意味着我需要检查数组中是否存在每个月才能访问它?我不想手动访问每个月。Hi随机使用另一个foreach month Year金额
Month | Year | Amount
August| 2019 | 226.3
July | 2019 | 747.1
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