【问题标题】:Merging array (Laravel Collection)合并数组(Laravel 集合)
【发布时间】:2017-03-07 12:54:14
【问题描述】:

我需要加入单独的数组,我正在使用 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"
    }
}

任何帮助或帮助将不胜感激。

【问题讨论】:

    标签: php arrays laravel


    【解决方案1】:

    尝试收藏zip()method

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

    【讨论】:

      猜你喜欢
      • 2018-09-22
      • 2018-07-22
      • 1970-01-01
      • 2020-04-09
      • 2016-05-19
      • 2020-12-29
      • 2022-09-23
      • 2021-07-20
      • 2016-06-05
      相关资源
      最近更新 更多