二维数组分组
1 /** 2 * 二维数组分组 3 * @param [type] $arr [二维数组] 4 * @param [type] $key [键名] 5 * @return [type] [新的二维数组] 6 */ 7 public function arrayGroupBy($arr, $key){ 8 $grouped = array(); 9 foreach ($arr as $value) { 10 $grouped[$value[$key]][] = $value; 11 } 12 if (func_num_args() > 2) { 13 $args = func_get_args(); 14 foreach ($grouped as $key => $value) { 15 $parms = array_merge($value, array_slice($args, 2, func_num_args())); 16 $grouped[$key] = call_user_func_array('array_group_by', $parms); 17 } 18 } 19 return $grouped; 20 }
分组前数据格式:
array:5 [ 0 => array:22 [ "name" => "Jackie Ma8" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-04-05/Thumbnail/b6d97aa1-1a90-42fd-80f2-1e33e41d808a.png" "last_company" => "广州陪我公司" "last_position" => "前端工程师" "job_type" => "风控专员" "channel" => "boss直聘" ] 1 => array:22 [ "name" => "Jackie Ma9" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-04-05/Thumbnail/b6d97aa1-1a90-42fd-80f2-1e33e41d808a.png" "last_company" => "广州陪我公司" "last_position" => "前端工程师" "job_type" => "风控专员" "channel" => "boss直聘" ] 2 => array:22 [ "name" => "Jackie Ma10" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-12-26/Thumbnail/5b4dc79d-81a1-494b-b2a5-be8164635ab3.png" "last_company" => "广州优才有限公司" "last_position" => "客服" "job_type" => "财务薪酬统计" "channel" => "智联招聘" ] 3 => array:22 [ "name" => "Jackie Ma12" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-12-26/Thumbnail/5b4dc79d-81a1-494b-b2a5-be8164635ab3.png" "last_company" => "广州优才有限公司" "last_position" => "客服" "job_type" => "财务薪酬统计" "channel" => "智联招聘" ] 4 => array:22 [ "name" => "Jackie Ma11" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-12-26/Thumbnail/5b4dc79d-81a1-494b-b2a5-be8164635ab3.png" "last_company" => "广州优才有限公司" "last_position" => "客服" "job_type" => "财务薪酬统计" "channel" => "智联招聘" ] ]
分组后数据格式:
array:2 [ "boss直聘" => array:2 [ 0 => array:22 [ "name" => "Jackie Ma8" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-04-05/Thumbnail/b6d97aa1-1a90-42fd-80f2-1e33e41d808a.png" "last_company" => "广州陪我公司" "last_position" => "前端工程师" "job_type" => "风控专员" "channel" => "boss直聘" ] 1 => array:22 [ "name" => "Jackie Ma9" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-04-05/Thumbnail/b6d97aa1-1a90-42fd-80f2-1e33e41d808a.png" "last_company" => "广州陪我公司" "last_position" => "前端工程师" "job_type" => "风控专员" "channel" => "boss直聘" ] ] "智联招聘" => array:3 [ 0 => array:22 [ "name" => "Jackie Ma10" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-12-26/Thumbnail/5b4dc79d-81a1-494b-b2a5-be8164635ab3.png" "last_company" => "广州优才有限公司" "last_position" => "客服" "job_type" => "财务薪酬统计" "channel" => "智联招聘" ] 1 => array:22 [ "name" => "Jackie Ma12" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-12-26/Thumbnail/5b4dc79d-81a1-494b-b2a5-be8164635ab3.png" "last_company" => "广州优才有限公司" "last_position" => "客服" "job_type" => "财务薪酬统计" "channel" => "智联招聘" ] 2 => array:22 [ "name" => "Jackie Ma11" "avatar" => "http://fooku.oss-cn-hongkong.aliyuncs.com/Content/UserHeadImage/2018-12-26/Thumbnail/5b4dc79d-81a1-494b-b2a5-be8164635ab3.png" "last_company" => "广州优才有限公司" "last_position" => "客服" "job_type" => "财务薪酬统计" "channel" => "智联招聘" ] ] ]