如何根据内部内容获取数组的总值
这是我的数组,
[Computers] => Array
(
[0] => Array
(
[product_id] => 78
[price] => 999.99
[quantity] => 2
)
[1] => Array
(
[product_id] => 70
[price] => 49.99
[quantity] => 6
)
)
[Scanners] => Array
(
[0] => Array
(
[product_id] => 65
[price] => 99.99
[quantity] => 7
)
[1] => Array
(
[product_id] => 70
[price] => 149.99
[quantity] => 5
)
[2] => Array
(
[product_id] => 70
[price] => 10.99
[quantity] => 1
)
)
我正在寻找一种方法根据内部数组来汇总外部数组的值
,例如 Computers
数组有两种产品,我需要根据数量获取总计。例如,计算机数组的 (999.99 *2) + (49.99 * 6) = 2299.92 等等......
here is my array
[Computers] => Array
(
[0] => Array
(
[product_id] => 78
[price] => 999.99
[quantity] => 2
)
[1] => Array
(
[product_id] => 70
[price] => 49.99
[quantity] => 6
)
)
[Scanners] => Array
(
[0] => Array
(
[product_id] => 65
[price] => 99.99
[quantity] => 7
)
[1] => Array
(
[product_id] => 70
[price] => 149.99
[quantity] => 5
)
[2] => Array
(
[product_id] => 70
[price] => 10.99
[quantity] => 1
)
)
I was looking for a way to total up the values of the outer arrays based on the inner arrays
so for example the Computers
array has two products and i need to get the totals based on quantity. So for example (999.99 *2) + (49.99 * 6) = 2299.92 for the computer array and so on...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个未经测试的解决方案,但我不明白为什么它不起作用
Here is a solution which is untested, but I don´t see why it shouldn´t work
这是一种替代方法,将产品映射到小计,然后对外部数组中每个项目的小计求和。
Here is an alternative approach which maps the products to sub-totals, then sums those sub-total for each of the items in the outer array.
试试这个代码:
Try this code: