dongtuo4132 2016-10-25 12:55
浏览 508

合并数组(Laravel Collection)

I need to join to separate arrays, I am working with Laravel 5.3 Collections. I have two separate objects. One object I am getting distance matrix from google maps between cities and street address. The end responses looks like this for example:

[{"distance":"14.0 km"},{"distance":"85.6 km"},{"distance":"15.9 km"}]

I have another response which lists the stores. The distance matrix above is relevant to each store. The store response looks like this:

{
    "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"
    }
}

I need to join each distance item into each store but I am having trouble combining the both. Basically I need the array to look like this:

{
    "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"
    }
}

Any help or assistance would be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanmu5039 2016-10-25 12:58
    关注

    try collection zip() method

    $distance = collect(["distance" => "14.0 km"]);
    
    $zipped = $distance->zip($stores);
    
    $zipped->all();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题