PHP 根据key 给二维数组分组

2018-06-22 05:02:42来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

我们经常拿到一个二维数组出来,会发现结果和自己想要的有些偏差,可能需要根据二维数组里的某个字段对数组分组。
先来看以下数组,

Array
(
    [0] => Array
        (
            [id] => 1
            [wo_id] => 2
            [evaluate_id] => 1
            [type] => 分组1
            [ctime] => 2016-12-02 11:39:34
        )

    [1] => Array
        (
            [id] => 2
            [wo_id] => 31
            [evaluate_id] => 2
            [type] => 分组1
            [ctime] => 2016-12-12 11:39:50
        )

    [2] => Array
        (
            [id] => 3
            [wo_id] => 31
            [evaluate_id] => 4
            [type] => 分组2
            [ctime] => 2016-12-02 11:40:01
        )

    [3] => Array
        (
            [id] => 4
            [wo_id] => 2
            [evaluate_id] => 5
            [type] => 分组2
            [ctime] => 2016-12-07 10:21:40
        )

    [4] => Array
        (
            [id] => 5
            [wo_id] => 2
            [evaluate_id] => 3
            [type] => 分组2
            [ctime] => 2016-12-07 10:21:51
        )

    [5] => Array
        (
            [id] => 6
            [wo_id] => 2
            [evaluate_id] => 2
            [type] => 分组3
            [ctime] => 2016-12-07 10:21:56
        )

    [6] => Array
        (
            [id] => 7
            [wo_id] => 4
            [evaluate_id] => 2
            [type] => 分组3
            [ctime] => 2016-12-07 10:22:10
        )
)    

这里是一个很典型的例子,如果我希望把 数组中的数据 根据 type(分组) 分开处理,如果不分组,可能循环上会非常冗余,所以有了下面的一个分组函数:

  public static function array_group_by($arr, $key)
    {
        $grouped = [];
        foreach ($arr as $value) {
            $grouped[$value[$key]][] = $value;
        }
        // Recursively build a nested grouping if more parameters are supplied
        // Each grouped array value is grouped according to the next sequential key
        if (func_num_args() > 2) {
            $args = func_get_args();
            foreach ($grouped as $key => $value) {
                $parms = array_merge([$value], array_slice($args, 2, func_num_args()));
                $grouped[$key] = call_user_func_array('array_group_by', $parms);
            }
        }
        return $grouped;
    }

$arr : 二维数组

$key: 需要分组的key

得到的结果是:

Array
(
    [分组2] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [wo_id] => 2
                    [evaluate_id] => 1
                    [ctime] => 2016-12-02 11:39:34
                    [type] => 分组2
                )

            [1] => Array
                (
                    [id] => 2
                    [wo_id] => 31
                    [evaluate_id] => 2
                    [ctime] => 2016-12-12 11:39:50
                    [type] => 分组2
                )

            [2] => Array
                (
                    [id] => 3
                    [wo_id] => 31
                    [evaluate_id] => 4
                    [ctime] => 2016-12-02 11:40:01
                    [type] => 分组2
                )

        )

    [分组1] => Array
        (
            [0] => Array
                (
                    [id] => 4
                    [wo_id] => 2
                    [evaluate_id] => 5
                    [ctime] => 2016-12-07 10:21:40
                    [type] => 分组1
                )

            [1] => Array
                (
                    [id] => 5
                    [wo_id] => 2
                    [evaluate_id] => 3
                    [ctime] => 2016-12-07 10:21:51
                    [type] => 分组1
                )
        )
)

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:移动微信支付报错总结(jsapi)

下一篇:安装pear及使用