gpt4 book ai didi

php - 将数组中的两列相乘并求出它们的总和

转载 作者:行者123 更新时间:2023-11-29 18:11:04 24 4
gpt4 key购买 nike

我有一个按数据库查询中的日期分组的数组结构(见下文),我希望通过将数量乘以交易金额来找到每个组的总数>.

这是我到目前为止所拥有的:

    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)

目的是获得按日期分组的所有交易的总数。

最佳答案

这可能不起作用,因为我不确定您是否需要使用日期作为键来访问交易...但是这样的东西会向每个数组项添加一个元素和总计。

// 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' =>

关于php - 将数组中的两列相乘并求出它们的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47401244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com