我有这个数组结构(见下文)按日期从数据库查询中分组,我希望通过将quantity
乘以transamount
来找到每个组的总数。
这是我到目前为止所做的:
array (size=3)
'2017-7-03' =>
array (size=3)
0 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'batch1' (length=31)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
1 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'Eye Test' (length=8)
'quantity' => string '1.0000' (length=6)
'transamount' => string '5.0000' (length=6)
2 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'hub photos' (length=16)
'quantity' => string '1.0000' (length=6)
'transamount' => string '82.0000' (length=7)
'2017-7-04' =>
array (size=9)
0 =>
array (size=4)
'paydate' => string '2017-7-04' (length=9)
'tariff' => string ' Register' (length=21)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
目的是获得按日期分组的所有交易总数。
答案 0 :(得分:0)
这可能不起作用,因为我不确定你是否需要使用日期作为键来访问事务......但是这样的事情会为每个数组项添加一个元素和总数。
// loop through the arrays sorted by date
foreach ($groups as $group)
{
$total = 0;
// loop through each transaction
foreach ($group as $transaction)
{
// Add to the total for each purchase group
$total += $transaction->quantity * $transaction->transamount;
}
// put the total into the group array
$group->total = $total
}
有了这个,你应该得到像
这样的东西'total' => (calculated value),
'2017-7-03' =>
array (size=3)
0 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'batch1' (length=31)
'quantity' => string '1.0000' (length=6)
'transamount' => string '20.0000' (length=7)
1 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'Eye Test' (length=8)
'quantity' => string '1.0000' (length=6)
'transamount' => string '5.0000' (length=6)
2 =>
array (size=4)
'paydate' => string '2017-7-03' (length=9)
'tariff' => string 'hub photos' (length=16)
'quantity' => string '1.0000' (length=6)
'transamount' => string '82.0000' (length=7)
'2017-7-04' =>