二维数组根据某个字段进行分组

/**
 * 二维数组根据某个字段进行分组
 * @param $arr
 * @param $key
 * @param string $col_id
 * @return array
 * created by Steven at 2020-7-31 11:56
 */
protected static function array_group_by($arr, $key, $col_id ='id'){
    $grouped = array();
    foreach ($arr as $value) {
        $grouped[$value[$key]][$value[$col_id]] = $value;
    }
    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;
}

你可能感兴趣的:(php,经验,笔记,PHP数组)