我想将2个数组合并为新键上的多个数组,并检查相同的键。这是在图像原始数组上,其中suborderid键是确定它们的键
数组1:($pso
)
Array
(
[0] => Array
(
[suborderid] => 127
[orderid] => 120
)
[1] => Array
(
[suborderid] => 128
[orderid] => 120
)
[2] => Array
(
[suborderid] => 129
[orderid] => 120
)
)
数组2:($items
)
Array
(
[0] => Array
(
[suborderid] => 127
[title] => Kue Nastar
)
[1] => Array
(
[suborderid] => 128
[title] => Kue Lapis
)
[2] => Array
(
[suborderid] => 128
[title] => Kue Bakar
)
)
这是我想要的结果
Array
(
[0] => Array
(
[suborderid] => 127
[orderid] => 120
[0] => Array
(
[suborderid] => 127
[title] => Kue Nastar
)
)
[1] => Array
(
[suborderid] => 128
[orderid] => 120
[0] => Array
(
[suborderid] => 128
[title] => Kue Lapis
)
[1] => Array
(
[suborderid] => 128
[title] => Kue Bakar
)
)
[2] => Array
(
[suborderid] => 129
[orderid] => 120
)
)
这是我到目前为止尝试过的:
foreach ($pso as $key => $i){
foreach ($items as $row => $l){
$pso[$key]['items'] = $items;
}
}
答案 0 :(得分:1)
$newPso = [];
foreach ($pso as $val) {
$newPso[$val["suborderid"]] = $val;
}
foreach ($items as $item) {
$newPso[$item["suborderid"]][] = $item;
}
$pso = array_values($newPso);
print_r($pso);
首先,我创建一个新数组,其值为$pso
数组及其子顺序键的键。
然后,我仅迭代$items
数组并添加值。 array_values
是为了摆脱新密钥。
答案 1 :(得分:0)
尝试一下
$newArr=$pso;
foreach ($pso as $key => $i){
foreach ($items as $row => $l){
if($i['suborderid']==$l['suborderid']){
$newArr[$key]['items']=$l;
}
}
}