在处理关联数组时,经常需要对多维数组按指定字段分组聚合,下面封装了一个数组分组聚合方法:

/**
 * 对数组进行分组聚合
 * @param $array
 * @param $keys
 * @return $result
 */
function array_group_by($array, $keys)
{
    if(!is_array($keys) || count($keys) == 1)
    {
        $key = is_array($keys) ? array_shift($keys) : $keys;

        return array_reduce($array, function($tmp_result, $item) use ($key)
        {
            $tmp_result[$item[$key]][] = $item;

            return $tmp_result;
        });
    }
    else
    {
        $keys = array_values($keys);

        $result = array_group_by($array, array_shift($keys));

        foreach ($result as $k=>$value)
        {
            $result[$k] = array_group_by($value, $keys);
        }

        return $result;
    }
}

测试:

$arr = [
     [
       "first" => "aa",
       "second" => "ccc",
       "third" => "sdfgg",
     ],
     [
       "first" => "aa",
       "second" => "ccc",
       "third" => "dddsa",
     ],
     [
       "first" => "aa",
       "second" => "sdfsdfd",
       "third" => "sdfgg",
     ],
     [
       "first" => "bb",
       "second" => "ccc",
       "third" => "sdfgg",
     ],
     [
       "first" => "bb",
       "second" => "sdfsdfd",
       "third" => "sdfgg",
     ],
   ];
    
    print_r(array_group_by($arr, ['first']));
    /*
    [
     "aa" => [
       [
         "first" => "aa",
         "second" => "ccc",
         "third" => "sdfgg",
       ],
       [
         "first" => "aa",
         "second" => "ccc",
         "third" => "dddsa",
       ],
       [
         "first" => "aa",
         "second" => "sdfsdfd",
         "third" => "sdfgg",
       ],
     ],
     "bb" => [
       [
         "first" => "bb",
         "second" => "ccc",
         "third" => "sdfgg",
       ],
       [
         "first" => "bb",
         "second" => "sdfsdfd",
         "third" => "sdfgg",
       ],
     ],
   ]
    */
    print_r(array_group_by($arr, ['first','second']));
    /*
    [
     "aa" => [
       "ccc" => [
         [
           "first" => "aa",
           "second" => "ccc",
           "third" => "sdfgg",
         ],
         [
           "first" => "aa",
           "second" => "ccc",
           "third" => "dddsa",
         ],
       ],
       "sdfsdfd" => [
         [
           "first" => "aa",
           "second" => "sdfsdfd",
           "third" => "sdfgg",
         ],
       ],
     ],
     "bb" => [
       "ccc" => [
         [
           "first" => "bb",
           "second" => "ccc",
           "third" => "sdfgg",
         ],
       ],
       "sdfsdfd" => [
         [
           "first" => "bb",
           "second" => "sdfsdfd",
           "third" => "sdfgg",
         ],
       ],
     ],
   ]
    */

标签: none

添加新评论