🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
原始数据中 ~~~ “table_name”:字段所属表 “field_name“ : 字段名 “field_col_num":字段排序号 ~~~ 期待输出的结果是 ~~~ { "table_name" : [ "field_name", "field_name", .... ] } ~~~ 原始数据: ``` [ { "table_name": "dp", "field_name": "dpcryptfeat", "field_col_num": "6" }, { "table_name": "dp", "field_name": "dppath", "field_col_num": "4" }, { "table_name": "dp", "field_name": "dpauthfeat", "field_col_num": "5" }, { "table_name": "dp", "field_name": "dpnick", "field_col_num": "3" }, { "table_name": "icc", "field_name": "iccbufsizeprot", "field_col_num": "13" }, { "table_name": "icc", "field_name": "iccmech", "field_col_num": "5" }, { "table_name": "icc", "field_name": "iccnick", "field_col_num": "3" }, { "table_name": "icc", "field_name": "iccprocmanager", "field_col_num": "8" }, { "table_name": "icc", "field_name": "iccdesc", "field_col_num": "4" }, { "table_name": "icc", "field_name": "iccprot", "field_col_num": "6" }, { "table_name": "icc", "field_name": "iccformatprot", "field_col_num": "14" } ] ``` 输出结果: ~~~ { "dp": [ "dpnick", "dppath", "dpauthfeat", "dpcryptfeat" ], "icc": [ "iccnick", "iccdesc", "iccmech", "iccprot", "iccprocmanager", "iccbufsizeprot", "iccformatprot" ] } ~~~ # 解决一 ~~~ dd(collect(json_decode($input))->groupBy('table_name')->map(function($item){ return collect($item)->sortBy('field_col_num')->map(function($data) { return $data->field_name; })->values(); })->toJson()); ~~~ # 解决二 ~~~ collect($groups)->sortBy('field_col_num')->groupBy('table_name')->map(function ($group) { return $group->pluck('field_name'); }); ~~~