Okay I have a script it opens a file and writes down some scores from a flash game. It works perfect for scoring high scores first and lower scores on the bottom. Some games require the lower score up top though like golf. This is where I'm having trouble getting it to score the lowest score first. Below is the code to score the high game anyone have an idea of what to do for the low score? if ($action == "INSERT1") { // Add name to end of list, and sort $tscores[$scoresize + 1][0] = $winscore; $tscores[$scoresize + 1][1] = $winname; rsort ($tscores); $file=fopen($filename, "w"); // Write them out for ($i = 0; $i < $scoresize; $i++) { $st = serialize($tscores[$i]) . "\n"; fputs($file, $st); } fclose($file); } PHP: The rsort function sorts the scores in reverse order but if I use the sort function in theory it would work fine but it doesn't work at all. Thanks
rsort is useful for linear arrays, while you are using a multidimensional array, therefore: array_multisort() is what you need
array_multisort() seems to work but.... the script is ment to show the top 10 people and if there is less then 10 scores the script just fills in the last scores with 0 and the name none. This is screwing up the script because it then sorts it ASC and the 0's are placed first. Here is the whole script I'm using <?php $winscore = (int)$_POST['winscore']; $filename = $_POST['filename']; $action = $_POST['action']; $scoresize = $_POST['scoresize']; $viewtype = $_POST['viewtype']; $winname = $_POST['winname']; if ($winname == "Guest" or $winname == "") { die("You must be logged in to be ranked"); } // Create a Blank File if it doesn't already exist if (!file_exists($filename)) { $file=fopen($filename, "w"); fclose ($file); } // Read the file in $oscores = file ($filename); $numreadin = count($oscores); // Break out the data into a new 2-d array called $tscores for ($i = 0; $i < $numreadin; $i++) { $g = unserialize($oscores[$i]); $tscores[$i][0] = $g[0]; $tscores[$i][1] = $g[1]; } // Fill in any missing data with none/0 for ($i = $numreadin; $i < $scoresize; $i++) { $tscores[$i][0] = 0; $tscores[$i][1] = "none"; } // Process the actions // Insert a score/name if ($action == "INSERT1") { // Add name to end of list, and sort $tscores[$scoresize + 1][0] = $winscore; $tscores[$scoresize + 1][1] = $winname; rsort ($tscores); $file=fopen($filename, "w"); // Write them out for ($i = 0; $i < $scoresize; $i++) { $st = serialize($tscores[$i]) . "\n"; fputs($file, $st); } fclose($file); } if ($action == "INSERT2") { // Add name to end of list, and sort $tscores[$scoresize + 1][0] = $winscore; $tscores[$scoresize + 1][1] = $winname; array_multisort($tscores, SORT_ASC); $file=fopen($filename, "w"); // Write them out for ($i = 0; $i < $scoresize; $i++) { $st = serialize($tscores[$i]) . "\n"; fputs($file, $st); } fclose($file); } // Clear the list if ($action == "CLEAR") { $k[0] = 0; $k[1] = "none"; $ser = serialize($k); $file=fopen($filename, "w"); for ($i = 0; $i < $scoresize; $i++) { $st = $ser . "\n"; fputs($file, $st); } fclose($file); } // Process the OUTPUT options if ($viewtype == "HTML") { // HTML PAGE CREATED HERE ?> <table cellpadding=2 cellspacing=2 border=0 width="152"> <tr align=center> <th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">#</font></th> <th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Name</font></th> <th bgcolor="#000033"><font color="#FFFFFF" face="Arial, Helvetica, sans-serif">Score</font></th> </tr> <? for ($i = 0; $i < $scoresize; $i++) { echo ("<tr bgcolor='#666666' align='center'><td><font size='2' face='Arial, Helvetica, sans-serif'>"); echo ($i + 1); echo ("</font></td><td><font size='2' face='Arial, Helvetica, sans-serif'>"); echo ($tscores[$i][1]); echo ("</font></td><td><font size='2' face='Arial, Helvetica, sans-serif'>"); echo ($tscores[$i][0]); echo ("</font></td></tr>"); } ?> </table> <? } // FLASH DATA CREATED HERE if ($viewtype == "FLASH") { for ($i = 0; $i < $scoresize; $i++) { echo ("NAME" . $i . "="); echo ($tscores[$i][1]); echo ("&SCORE" . $i . "="); echo ($tscores[$i][0]); echo ("&"); } } ?> PHP: