我有一个脚本从mysql数据库获取订单并返回一个名为orders
的多维数组,当我执行print_r($orders)
时格式如下。
正如您所看到的那样,订购的商品被分组为每个订单,并作为包含某些商品详情的数组。
Array
(
[0] => Array
(
[order_id] => 2646
[0] => Array
(
[code] => 2811
[qty] => 1
[weight] => 6500
)
[1] => Array
(
[code] => 2812A
[qty] => 3
[weight] => 10000
)
)
[1] => Array
(
[order_id] => 2647
[0] => Array
(
[code] => 2812A
[qty] => 2
[weight] => 10000
)
[1] => Array
(
[code] => 2810
[qty] => 2
[weight] => 10800
)
)
[2] => Array
(
[order_id] => 2650
[0] => Array
(
[code] => 2906
[qty] => 1
[weight] => 12700
)
[1] => Array
(
[code] => 2908
[qty] => 6
[weight] => 11900
)
[2] => Array
(
[code] => 2909
[qty] => 3
[weight] => 11400
)
[3] => Array
(
[code] => 2913
[qty] => 1
[weight] => 11000
)
)
[3] => Array
(
[order_id] => 2658
[0] => Array
(
[code] => 2880
[qty] => 3
[weight] => 10900
)
[1] => Array
(
[code] => 2881
[qty] => 3
[weight] => 8100
)
[2] => Array
(
[code] => 2882
[qty] => 1
[weight] => 12700
)
)
)
我要做的是重新编写$orders
数组,这样看起来就像下面的那样,有序项被合并到父数组中,这些项的索引从1开始计数。
Array
(
[0] => Array
(
[order_id] => 2646
[code_1] => 2811
[qty_1] => 1
[weight_1] => 6500
[code_2] => 2812A
[qty_2] => 3
[weight_2] => 10000
)
[1] => Array
(
[order_id] => 2647
[code_1] => 2812A
[qty_1] => 2
[weight_1] => 10000
[code_2] => 2810
[qty_2] => 2
[weight_2] => 10800
)
[2] => Array
(
[order_id] => 2650
[code_1] => 2906
[qty_1] => 1
[weight_1] => 12700
[code_2] => 2908
[qty_2] => 6
[weight_2] => 11900
[code_3] => 2909
[qty_3] => 3
[weight_3] => 11400
[code_4] => 2913
[qty_4] => 1
[weight_4] => 11000
)
[3] => Array
(
[order_id] => 2658
[code_1] => 2880
[qty_1] => 3
[weight_1] => 10900
[code_2] => 2881
[qty_2] => 3
[weight_2] => 8100
[code_3] => 2882
[qty_3] => 1
[weight_3] => 12700
)
)
我已经尝试过如此多的代码和功能片段,我要么感到困惑,要么完全偏离轨道。任何反馈或建议都非常感谢。