这是我的数据
$dataItems = [
[
'id' => 1,
'serial' => "XXXXXXAA1",
'pd_item_info' =>
[
'id' => 1,
'quantity' => 5,
'ipo_item_pml_info' =>
[
'id' => 1,
'product_name' => 'Keyboard'
]
]
],
[
'id' => 2,
'serial' => "XXXXXXAA2",
'pd_item_info' =>
[
'id' => 2,
'quantity' => 10,
'ipo_item_pml_info' =>
[
'id' => 2,
'product_name' => 'Keyboard'
]
]
]
];
并且我需要输出:
$output = [
'product_name' => "Keyboard",
'serial' =>["XXXXXXAA1", "XXXXXXAA2"]
];
在laravel中使用foreach
和array_push
,如何合并重复的product_name
并在那里串行连接?
答案 0 :(得分:1)
我假设pd_item_info
和ipo_item_pml_info
一直在那里
首先根据产品名称区分阵列键,所有序列号都附加在同一产品名称下
foreach($dataItems as $item) {
$products[$item['pd_item_info']['ipo_item_pml_info']['product_name']][] = $item['serial'];
}
//output
// [
// 'Keyboard' => ['XXXXXXAA1', 'XXXXXXAA2'];
// ]
下一步,根据您的需要进行格式化
foreach($products as $key => $product) {
$output[] = [
'product_name' => $key,
'serial' => $product
];
}
就这样... = D