//以$key_array进行分组(可以是单个字段分组, 也可以多个字段组合分组), 计算平均分, 计算排名(同分同排名)
public static function avg_by_array($list, $key_score = "zh_score", $key_array = "depart_id", $sort = SORT_DESC){
$list_new = [];
foreach ($list as $rrr){
if(is_array($key_array)){ //以多个字段 进行分组
$temp = array_fill_keys($key_array,'');
$result=array_intersect_key($rrr,$temp);
$temp_key = implode("-", $result);
}else{ //单个字段分组, 例如depart_id
$temp_key = $rrr[$key_array];
}
// $temp_key = $rrr[$key];//以depart_id分组
if(!isset($list_new[$temp_key]['compute_sum'])){ //第一次成绩
$list_new[$temp_key] = $rrr;
$list_new[$temp_key]['compute_sum'] = (float)$rrr[$key_score];
$list_new[$temp_key]['compute_count'] = 1;
$list_new[$temp_key][$key_score] = (float)$rrr[$key_score];
}else{ //累加成绩,算平均分
$list_new[$temp_key]['compute_sum'] = round($list_new[$temp_key]['compute_sum'] + $rrr[$key_score],2);
$list_new[$temp_key]['compute_count'] += 1;
$score = round($list_new[$temp_key]['compute_sum'] / $list_new[$temp_key]['compute_count'],2); ;
$list_new[$temp_key][$key_score] = (float)$score;
}
}
$score = array_column($list_new,$key_score);
array_multisort($score, $sort, $list_new);
$pre_score = '';
$count = 1;
$rank ='';
foreach ($list_new as $key => $row){
if($pre_score != $row[$key_score]){
$pre_score = $row[$key_score];
$rank = $count;
}
$row['rank'] = $rank;
$list_new[$key] = $row;
$count++;
}
return $list_new;
}