我需要加入单独的数组,我正在使用 Laravel 5.3 Collections。我有两个独立的对象。我从谷歌地图中获取城市和街道地址之间的距离矩阵的一个对象。最终响应如下所示:
[{"distance":"14.0 km"},{"distance":"85.6 km"},{"distance":"15.9 km"}]
我有另一个回复,其中列出了商店。上面的距离矩阵与每个商店相关。商店的回复如下所示:
{
"0":{
"id":3,
"user_id":2,
"brand_id":"2",
"type":"store",
"name":"Store One"
},
"4":{
"id":6,
"user_id":2,
"brand_id":"2",
"type":"store",
"name":"Store Two"
},
"6":{
"id":9,
"user_id":2,
"brand_id":"2",
"type":"store",
"name":"Store Three"
}
}
我需要将
each
距离项目加入每个商店,但我在将两者结合起来时遇到困难。基本上我需要的数组看起来像这样:
{
"0":{
"id":3,
"user_id":2,
"brand_id":"2",
"type":"store",
"name":"Store One",
"distance":"14.0 km"
},
"4":{
"id":6,
"user_id":2,
"brand_id":"2",
"type":"store",
"name":"Store Two",
"distance":"85.6 km"
},
"6":{
"id":9,
"user_id":2,
"brand_id":"2",
"type":"store",
"name":"Store Three",
"distance":"15.9 km"
}
}
任何帮助或协助将不胜感激。
尝试收藏
zip()
方法
$distance = collect(["distance" => "14.0 km"]);
$zipped = $distance->zip($stores);
$zipped->all();