Laravel从多维集合中展平并采摘 [英] Laravel flatten and pluck from multidimensional collection

查看:63
本文介绍了Laravel从多维集合中展平并采摘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从给定的集合中仅检索一个 id 数组,例如 [10,54,61,21,etc] .我尝试了 flatten pluck ,但是除了 foreach 似乎没有其他工作,我想在此步骤中删除该东西.

I have to retrieve just an array of id from the given collection, something like [10,54,61,21,etc]. I've tried flatten, pluck, but nothing seems to work apart from a foreach which is something I would like to remove at this step.

// Model
class Children extends Eloquent {
    public function directChildrens(){
        return $this->hasMany('App\Children','father_id','id')->select('id','father_id');
    }

    public function childrens(){
        return $this->directChildrens()->with('childrens');
    }
}

// Controller
return $children->childrens()->get();

按预期,它可以正常工作.结果如下:

As expected it works fine. Here a result:

[{
"id": 10,
"father_id": 2,
"childrens": [
    {
        "id": 54,
        "father_id": 10,
        "childrens": [
            {
                "id": 61,
                "father_id": 54,
                "childrens": []
            }
        ]
    },
    {
        "id": 21,
        "father_id": 10,
        "childrens": []
    }
]
}]

如何获取此集合的 pluck('id')以获得 [10,54,61,21] ?

How can I perform a pluck('id') of this collection in order to get [10,54,61,21] ?

推荐答案

$result = [
            [
                "id" => 10,
                "father_id" => 2,
                "childrens" => [
                    [
                        "id" => 54,
                        "father_id" => 10,
                        "childrens" => [
                            [
                                "id" => 61,
                                "father_id" => 54,
                                "childrens" => []
                            ]
                        ]
                    ],
                    [
                        "id" => 21,
                        "father_id" => 10,
                        "childrens" => []
                    ]
                ]
            ]
        ];

return collect($result)
        ->map(function ($value) {
            return Arr::dot($value); // converts it to the dot notation version (added the example at the end)
        })
        ->collapse() // collapse them into a single one
        ->filter(function ($value, $key) {
            return $key === 'id' || Str::endsWith($key, '.id'); // filter first id + patterns of .id
        })
        ->values() // discard dot notation keys
        ->toArray();

Arr :: dot()方法将集合转换为以下格式,将所有格式展平到同一级别.

Arr::dot() method turned collection into the following format which flattens all into the same level.

[
  {
    "id": 10,
    "father_id": 2,
    "childrens.0.id": 54,
    "childrens.0.father_id": 10,
    "childrens.0.childrens.0.id": 61,
    "childrens.0.childrens.0.father_id": 54,
    "childrens.0.childrens.0.childrens": [],
    "childrens.1.id": 21,
    "childrens.1.father_id": 10,
    "childrens.1.childrens": []
  }
]

感谢@Dan关于已弃用/删除的函数 array_dot 的警告.替换为 Arr :: dot()

Thanks to @Dan warning about deprecated/removed function array_dot. Replaced with Arr::dot()

这篇关于Laravel从多维集合中展平并采摘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆