通过CakePHP中的Collection来组织另一个groupBy [英] Organize a groupBy within another groupBy using Collection in CakePHP

查看:96
本文介绍了通过CakePHP中的Collection来组织另一个groupBy的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数组,我希望能够通过收藏来组织它

I have the following array and I would like to be able to organize it through collection

$collection =  Collection($total);
$filter = $collection->groupBy('date_general')->toArray();

[2017-11-14] => Array
    (
        [0] => Array
            (
                [status] => pending
                [date_general] => 2017-11-14
                [user_id] => 164
            )

        [1] => Array
            (
                [status] => pending
                [date_general] => 2017-11-14
                [user_id] => 112
            )
    )

到目前为止,我已经按日期组织了数组。现在,在每个日期之内,我需要按用户组织它,也就是说,它是:

Up to this point I already have the array organized by dates. Now within each date I need to organize it by user, that is to say that it is:

[2017-11-14] => Array
    (
        [164] => Array
            (
                [0] => Array
                    (
                        [status] => pending
                        [date_general] => 2017-11-14
                        [user_id] => 164
                    )
            )
        [112] => Array
            (
               [0] => Array
                (
                    [status] => pending
                    [date_general] => 2017-11-14
                    [user_id] => 112
                )
            )
    )


推荐答案

您必须处理每个日期组并将其内容分别分组。

You'll have to process each date group and individually group their contents.

$collection = collection($total)
    ->groupBy('date_general')
    ->map(function ($data) {
        return collection($data)->groupBy('user_id')->toArray();
    });

另请参见

这篇关于通过CakePHP中的Collection来组织另一个groupBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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