使用CakePHP中的Collection,在另一个组中组织一个groupBy

时间:2017-11-15 10:07:21

标签: cakephp collections cakephp-3.x

我有以下数组,我希望能够通过集合

组织它
$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
            )
    )

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

[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
                )
            )
    )

1 个答案:

答案 0 :(得分:4)

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

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

另见