如果标题不够明确,我很抱歉。我有以下调试数组:
Array
(
[output] => Array
(
[0] => Array
(
[item] => Gear 2 (1PA-1111-00)
[item_number] => 1PA-1111-00
[item_id] => 227
[routes] => Array
(
[227-DELIVERY] => Array
(
[line_name] => Distribution
[pid] => 48
[rid] => 605
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] => + 1PA-E5514-00
[selected_capacity] => 1500
[capacity] => 1500
[capacity_ot2] => 0
[capacity_ot4] => 0
[dates] => Array
(
[2015-12-15] => 1200
)
)
[227-Inspection] => Array
(
[line_name] => QC
[pid] => 35
[rid] => 606
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 1500
[capacity] => 1500
[capacity_ot2] => 0
[capacity_ot4] => 0
[dates] => Array
(
[2015-12-14] => 1200
)
)
[227-N/C Finishing ] => Array
(
[line_name] => Line 33
[pid] => 29
[rid] => 607
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 2793
[capacity] => 2793
[capacity_ot2] => 3142
[capacity_ot4] => 3491
[dates] => Array
(
[2015-12-11] => 1200
)
)
[227-Heat Treatment] => Array
(
[line_name] => Line 32
[pid] => 26
[rid] => 608
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 1024
[capacity] => 1024
[capacity_ot2] => 0
[capacity_ot4] => 0
[dates] => Array
(
[2015-12-08] => 1024
[2015-12-07] => 1024
)
)
[227-Hobbing] => Array
(
[line_name] => Line 12
[pid] => 5
[rid] => 609
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 4352
[capacity] => 4352
[capacity_ot2] => 4896
[capacity_ot4] => 5440
[dates] => Array
(
[2015-12-04] => 2048
)
)
[227-Shaper] => Array
(
[line_name] => Line 12
[pid] => 9
[rid] => 610
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 2591
[capacity] => 2591
[capacity_ot2] => 2915
[capacity_ot4] => 3239
[dates] => Array
(
[2015-12-03] => 2048
)
)
[227-Incoming] => Array
(
[line_name] => QC
[pid] => 38
[rid] => 611
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 500
[capacity] => 500
[capacity_ot2] => 562
[capacity_ot4] => 624
[dates] => Array
(
[2015-12-01] => 500
[2015-11-30] => 500
[2015-11-27] => 500
[2015-11-26] => 500
[2015-11-25] => 500
)
)
[227-LATHE MATERIAL] => Array
(
[line_name] => Outsource
[pid] => 50
[rid] => 612
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 500
[capacity] => 500
[capacity_ot2] => 2915
[capacity_ot4] => 3239
[dates] => Array
(
[2015-12-01] => 500
[2015-11-30] => 500
[2015-11-27] => 500
[2015-11-26] => 500
[2015-11-25] => 500
)
)
[227-FORGING MATERIAL] => Array
(
[line_name] => Outsource
[pid] => 52
[rid] => 613
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 500
[capacity] => 500
[capacity_ot2] => 2915
[capacity_ot4] => 3239
[dates] => Array
(
[2015-12-01] => 500
[2015-11-30] => 500
[2015-11-27] => 500
[2015-11-26] => 500
[2015-11-25] => 500
)
)
)
[dates] => Array
(
[0] => Array
(
[delivery_date] => 2015-12-18
[qty] => 1000
)
[1] => Array
(
[delivery_date] => 2015-12-21
[qty] => 200
)
)
)
[1] => Array
(
[item] => Gear 2 (1PA-1111-00)
[item_number] => 1PA-1111-00
[item_id] => 227
[routes] => Array
(
[227-DELIVERY] => Array
(
[line_name] => Distribution
[pid] => 48
[rid] => 605
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] => + 1PA-E5514-00
[selected_capacity] => 1500
[capacity] => 1500
[capacity_ot2] => 0
[capacity_ot4] => 0
[dates] => Array
(
[2015-12-16] => 1200
)
)
[227-Inspection] => Array
(
[line_name] => QC
[pid] => 35
[rid] => 606
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 1500
[capacity] => 1500
[capacity_ot2] => 0
[capacity_ot4] => 0
[dates] => Array
(
[2015-12-15] => 1200
)
)
[227-N/C Finishing ] => Array
(
[line_name] => Line 33
[pid] => 29
[rid] => 607
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 2793
[capacity] => 2793
[capacity_ot2] => 3142
[capacity_ot4] => 3491
[dates] => Array
(
[2015-12-14] => 1200
)
)
[227-Heat Treatment] => Array
(
[line_name] => Line 32
[pid] => 26
[rid] => 608
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 1024
[capacity] => 1024
[capacity_ot2] => 0
[capacity_ot4] => 0
[dates] => Array
(
[2015-12-11] => 1024
[2015-12-10] => 1024
)
)
[227-Hobbing] => Array
(
[line_name] => Line 12
[pid] => 5
[rid] => 609
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 4352
[capacity] => 4352
[capacity_ot2] => 4896
[capacity_ot4] => 5440
[dates] => Array
(
[2015-12-09] => 2048
)
)
[227-Shaper] => Array
(
[line_name] => Line 12
[pid] => 9
[rid] => 610
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 2591
[capacity] => 2591
[capacity_ot2] => 2915
[capacity_ot4] => 3239
[dates] => Array
(
[2015-12-08] => 2048
)
)
[227-Incoming] => Array
(
[line_name] => QC
[pid] => 38
[rid] => 611
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 500
[capacity] => 500
[capacity_ot2] => 562
[capacity_ot4] => 624
[dates] => Array
(
[2015-12-04] => 500
[2015-12-03] => 500
[2015-12-02] => 500
[2015-12-01] => 500
[2015-11-30] => 500
)
)
[227-LATHE MATERIAL] => Array
(
[line_name] => Outsource
[pid] => 50
[rid] => 612
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 500
[capacity] => 500
[capacity_ot2] => 2915
[capacity_ot4] => 3239
[dates] => Array
(
[2015-12-04] => 500
[2015-12-03] => 500
[2015-12-02] => 500
[2015-12-01] => 500
[2015-11-30] => 500
)
)
[227-FORGING MATERIAL] => Array
(
[line_name] => Outsource
[pid] => 52
[rid] => 613
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] =>
[selected_capacity] => 500
[capacity] => 500
[capacity_ot2] => 2915
[capacity_ot4] => 3239
[dates] => Array
(
[2015-12-04] => 500
[2015-12-03] => 500
[2015-12-02] => 500
[2015-12-01] => 500
[2015-11-30] => 500
)
)
)
[dates] => Array
(
[0] => Array
(
[delivery_date] => 2015-12-18
[qty] => 1000
)
[1] => Array
(
[delivery_date] => 2015-12-21
[qty] => 200
)
)
)
)
[min_date] => 2015-11-25
[max_date] => 2015-12-21
)
正如您所看到的,除了$routes[ROUTE_NAME][dates]
中的日期之外,两个数组元素都是相同的,我想将这些值合并为一个,这样只有一个值,除了日期之外。
例如,输出ROUTE_NAME(eq.227-DELIVERY)
[227-DELIVERY] => Array
(
[line_name] => Distribution
[pid] => 48
[rid] => 605
[level] => 1
[item_id] => 227
[subpart_id] => 227
[subpart_name] => + 1PA-E5514-00
[selected_capacity] => 1500
[capacity] => 1500
[capacity_ot2] => 0
[capacity_ot4] => 0
[dates] => Array
(
[2015-12-15] => 1200
[2015-12-16] => 1200
)
)
有没有内置的php功能呢?我不认为array_merge_recursive可以做到这一点。
答案 0 :(得分:0)
array_merge_recursive
可能很麻烦,但简单的array_merge
与foreach
相结合会起作用。这将创建包含合并数据的新数组$result
:
$result = array();
foreach($array['output'][0]['routes'] as $key => $route){
$route['dates'] = array_merge($route['dates'], $array['output'][1]['routes'][$key]['dates']);
$result[$key] = $route;
}
print_r($result);
如果你想将所有内容都放在同一个数组中,你可以这样做:
foreach($array['output'][0]['routes'] as $key => &$route){
$route['dates'] = array_merge($route['dates'], $array['output'][1]['routes'][$key]['dates']);
}
print_r($array['output'][0]['routes']);
在foreach循环中注意&
,它基本上告诉我们修改我们正在循环的现有数组。