$v) { $score = 0; for ($i = 0; $i < count($L); $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; // $board[$i][$personB] = $board[$j][$personB]; // $board[$j][$personA] = $board[$i][$personA]; // unset($board[$j][$personB]); // unset($board[$i][$personA]); } } } if (DEBUG) var_dump($board); for ($i = 0; $i < $people; $i++) { echo $i + 1; echo " "; echo $board[$i]['name']; echo " "; for ($j = 0; $j < count($L); $j++) { echo $board[$i][$j] ?: 0; echo " "; } echo $board[$i]['score']; echo PHP_EOL; } function getIntP($s) { return ord($s) - ord('A'); }