独特的收藏Laravel

发布于 2025-01-23 10:41:41 字数 2598 浏览 0 评论 0原文

知道如何在这个Laravel集合中不同的价值

我 因此,我可以在集合中使其独特而沮丧的值

这就是我的理解方式:

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {

            return (object) [
                $categorie->categorie->map(function ($items) {
                    
                    return $items;
                })
            ];
        });

这就是结果!

 Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Fongicides et assimilés
                                        )
        
                                )
        
                        )
        
                    [1] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Autres
                                        )
        
                                )
        
                        )
        
                    [2] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Insecticides et assimilés
                                            [1] => Fongicides et assimilés
                                        )
        
                                )
        
                        )
        
                    [3] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Autres
                                        )
        
                                )
        
                        )
        
                )
        
        )

I wonder how to distinct values in this Laravel collection because there is sub array of objects

There is too much sub-arrays, but i don't know how to filter this collection to get less sub-arrays

I wish to get less sub-arrays so i could make it unique and disctinct values within the collection

This is how i get this :

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {

            return (object) [
                $categorie->categorie->map(function ($items) {
                    
                    return $items;
                })
            ];
        });

This is the result !

 Illuminate\Support\Collection Object
        (
            [items:protected] => Array
                (
                    [0] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Fongicides et assimilés
                                        )
        
                                )
        
                        )
        
                    [1] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Autres
                                        )
        
                                )
        
                        )
        
                    [2] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Insecticides et assimilés
                                            [1] => Fongicides et assimilés
                                        )
        
                                )
        
                        )
        
                    [3] => stdClass Object
                        (
                            [0] => Illuminate\Support\Collection Object
                                (
                                    [items:protected] => Array
                                        (
                                            [0] => Autres
                                        )
        
                                )
        
                        )
        
                )
        
        )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界等同你 2025-01-30 10:41:41

Laravel收藏品有很多选项

我认为您正在寻找 flatten


除了您应该更改代码,第一个返回对象[...]给出了一个奇怪的结果。
我认为您想要以下内容:

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {
    return $categorie->categorie->map(function ($items) {
        return $items;
    });
})->flatten();

编辑:

可以缩短到:

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {
    return $categorie->categorie;
})->flatten();

Laravel collections have a lot of options

I think you're looking for flatten


Besides that you should change your code a little bit, the first return object [...] gives a weird result.
I think you want the following:

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {
    return $categorie->categorie->map(function ($items) {
        return $items;
    });
})->flatten();

Edit:

That can be shortened to:

$CategorieTree = $CategoriesItineraires->map(function ($categorie) {
    return $categorie->categorie;
})->flatten();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文