【问题标题】:Laravel: insert a new key:value to collection in child arrayLaravel:插入一个新的键:值到子数组中的集合
【发布时间】:2018-08-20 11:30:32
【问题描述】:

您好,我有一个集合,我想在其中插入一个新的键和值

这是我的尝试

 $interpreters = $interpreters->map(function ($interpreter) use ($minimumPingValue) {

        $interpreter['opi_ping'] = $minimumPingValue;
    });

但它没有在集合中添加新的键值。

结果是一样的

{
    "error": false,
    "message": "Interpreters list.",
    "data": {
        "interpreters": [
            {
                "id": "6",
                "first_name": "Khizar",
                "display_picture": "https://d2bsp66iispep6.cloudfront.net/9/avatars/63178ac704787578f2ec773048584b3b.jpeg",
                "rating": 5
            },
            {
                "id": "25",
                "first_name": "AJ",
                "display_picture": null,
                "rating": 4
            },
            {
                "id": "26",
                "first_name": "Ejaz",
                "display_picture": null,
                "rating": null
            }
        ],
        "call_tracking_id": "CTF1ZPJY6BKs2KEpzgDsD5aa6733e2bab4"
    },
    "status_code": 0
}

【问题讨论】:

    标签: php laravel collections eloquent


    【解决方案1】:

    您在 map 调用中使用了闭包,因此您应该返回正在更改的变量,例如:

    $interpreters = $interpreters->map(function ($interpreter) use ($minimumPingValue) {
            $interpreter['opi_ping'] = $minimumPingValue;
            return $interpreter;
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 1970-01-01
      • 2021-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 2018-07-22
      • 1970-01-01
      相关资源
      最近更新 更多