$v) { $score = 0; for ($i = 0; $i < $cntL; $i++) { $score += $ans[$name][$i]; } $ans[$name]['score'] = $score; } $board = array(); foreach ($ans as $name => $v) { $base = $ans[$name]; $base['name'] = $name; $board[] = $base; } if (DEBUG) var_dump($board); $people = count($board); for ($i = 0; $i < $people - 1; $i++) { for ($j = $i + 1; $j < $people; $j++) { $swap1 = $board[$i]['score'] < $board[$j]['score']; $swap2a = $board[$i]['score'] == $board[$j]['score']; $swap2b = $board[$i]['lastmod'] > $board[$j]['lastmod']; if ($swap1 || ($swap2a && $swap2b)) { $tmp = $board[$i]; $board[$i] = $board[$j]; $board[$j] = $tmp; } } } if (DEBUG) var_dump($board); $output = ""; for ($i = 0; $i < $people; $i++) { $output .= $i + 1; $output .= " "; $output .= $board[$i]['name']; $output .= " "; for ($j = 0; $j < $cntL; $j++) { $output .= $board[$i][$j] ?: 0; $output .= " "; } $output .= $board[$i]['score']; $output .= PHP_EOL; } echo $output;