【发布时间】:2018-03-07 18:51:13
【问题描述】:
您好,我有一个具有共同值的关联数组。我想根据不同的值对这些值和组求和。
我的数组如下所示。
Array
(
[supercategory] => Food
[subtotal] => 1152.50
[discount] => 10.0000
[orderdiscount] => 10.00
[itemdiscount] => 0.00
[Total] => 1142.50
[Tax] => 80.06
[taxinclusive] => 0.00
[taxexclusive] => 80.06
[gratuity] => 0.00
[totalsale] => 1222.56
)
Array
(
[supercategory] => Drinks
[subtotal] => 1152.50
[discount] => 10.0000
[orderdiscount] => 10.00
[itemdiscount] => 0.00
[Total] => 1142.50
[Tax] => 80.06
[taxinclusive] => 0.00
[taxexclusive] => 80.06
[gratuity] => 0.00
[totalsale] => 1222.56
)
Array
(
[supercategory] => Food
[subtotal] => 1152.50
[discount] => 10.0000
[orderdiscount] => 10.00
[itemdiscount] => 0.00
[Total] => 1142.50
[Tax] => 80.06
[taxinclusive] => 0.00
[taxexclusive] => 80.06
[gratuity] => 0.00
[totalsale] => 1222.56
)
Array
(
[supercategory] => Alcohols
[subtotal] => 1152.50
[discount] => 10.0000
[orderdiscount] => 10.00
[itemdiscount] => 0.00
[Total] => 1142.50
[Tax] => 80.06
[taxinclusive] => 0.00
[taxexclusive] => 80.06
[gratuity] => 0.00
[totalsale] => 1222.56
)
在遍历这个数组时,我想根据键 supercategory 对 foreach 循环/While 中的数组值求和,以便我可以对具有相同值的所有这些键求和 totalsale。 请帮我解决这个问题。
【问题讨论】:
-
我们不做你的功课。见this link。
-
您在哪一部分遇到了问题?
-
@AlexKarshin 这很粗鲁。我的意思是你可以看到我是 Stackoverflow 的新手。你可以看到我以前的问题,我已经发布了我试图做的事情。但这一次我完全被卡住了。基本上我只需要知道如何将之前的数组值与接下来的数组值进行比较,然后我可以进行 SUM 并将它们自己分组。我的意思是,如果你能帮我发起一些事情,那么这里出了什么问题?
-
@Don'tPanic 我只需要知道如何将前一个数组值与下一个数组值进行比较,然后我可以进行 SUM 并自己分组。
标签: php foreach associative-array