public function score() { $rateTotal = 25;//总评分人数 $rateScore = 7.5;//最终评分 10分制 $totalScore = intval($rateTotal * $rateScore / 2); //总评分 转化为5分制 $percent = $rateScore / 10; $grade = [5, 4, 3, 2, 1]; $data = []; foreach ($grade as $item) { if ($item == 1) { $num = intval($totalScore); } else { $num = intval($totalScore * $percent / $item);//向下取整,就不用考虑数据超出的问题 } $totalScore = $totalScore - $num * $item; $data[$item] = $num; } var_dump($data); }