好吧,很抱歉我之前的回答不对,这一个是对的,我假设有两件事:
-
您的输入可以超过两行,并且可以有多个订单ID
-
我正在给id分配第一个具有该订单id的id(如果不是,则只能分配id键)。
代码如下:
$arrays = [
[
"id" => 1,
"order_id" => 1,
"image_url" => "/resources/filename1"
],
[
"id" => 2,
"order_id" => 1,
"image_url" => "/resources/filename2"
],
[
"id" => 3,
"order_id" => 1,
"image_url" => "/resources/filename3"
],
[
"id" => 4,
"order_id" => 2,
"image_url" => "/resources/filename4"
],
];
foreach($arrays as $array) {
if(!isset($result[$array['order_id']]['id'])) {
$result[$array['order_id']]['id']=$array['id'];
}
$result[$array['order_id']]['order_id']=$array['order_id'];
$result[$array['order_id']]['image_url'][]=$array['image_url'];
}
print_r($result);
测试
here
.