php多数组分组Array [0]
和每组最大Array [3]
值。以下数组表示Array [0]
- id,Array [1]
- price,Array [2]
- product name,Array [3]
- shipping price,Array [4]
- shipping method每个数组需要按id分组,然后对每组价格求和,并使用最大运费。输出如下所示。
Array
(
[0] => Array
(
[0] => 2
[1] => 2200.00
[2] => Portable Hard Drive
[3] => 120.00
[4] => Taxi
)
[1] => Array
(
[0] => 2
[1] => 8500.00
[2] => HP Laptop
[3] => 80.00
[4] => Taxi
)
[2] => Array
(
[0] => 1
[1] => 700.00
[2] => Luxury Watch
[3] => 200.00
[4] => TAXI FAST
)
[3] => Array
(
[0] => 2
[1] => 1500.00
[2] => VGA DISTRIBUTE
[3] => 300.00
[4] => EMS
)
[4] => Array
(
[0] => 1
[1] => 5000.00
[2] => 3D Camera
[3] => 150.00
[4] => DHL
)
)
输出列表如下所示,也可以放入数组中
1-5000.00-3D Camera-150.00-DHL
1-700.00-Luxury Watch-200.00-TAXI FAST
Total price = 5700.00
Max of ship = 200.00
Grand total = 5900.00
2-2200.00-Portable Hard Drive-120.00-taxi
2-8500.00-HP Laptop-80.00-Taxi
2-1500.00-VGA DISTRIBUTE-300.00-EMS
Total price = 12200.00
Max of ship = 300.00
Grand total = 12500.00
This order Total price 17900.00
This order Total max ship 500.00
This order Grand Total 18400.00
1条答案
按热度按时间cngwdvgl1#
要实现这一点,除了
foreach
之外,您不需要太多的array_column
、array_sum
和max
。把任务分成几个小任务:
按ID创建组并按ID排序后的初始$result:
$result计算完所有需要的数据后(这包含了我们需要显示它的所有内容):
输出:
这与您的输出之间的唯一区别是在group id中:1我有
Luxury Watch
第一和3D Camera
第二,这是你的输入数组的顺序。2顺序相同。