PHP Rank

Associate
Joined
26 Apr 2009
Posts
204
I'm trying to rank a group of scores. So I've taken the input from a users form and sorted them from largest to smallest in a multidimensional array, so then that I can search the array for the user and retrieve the index to get its "rank" however I appear to be doing something totally wrong! any help on where I'm going wrong would be greatly appreciated, or if there is a better way entirely!

Thank you

PHP:
if(isset($_POST['submit']))
{

    $size = $num_rows;
    $p = 0;
    $myarray = array();

    while($p < $size) 
    {
      $myarray[] = array("user" => $user[$p], "data" => $scanrate[$p]);
      $p++;
    }

    $sort = array();

    foreach($myarray as $k=>$v) 
    {
        $sort['data'][$k] = $v['data'];
    }

    array_multisort($sort['data'], SORT_DESC, $myarray);

    for($i=0;$i<$num_rows;$i++)
    {
        $key = array_search($i, array_column($myarray, 'user'));

        if(!$key)
        {
            $key = $num_rows;
        }
        //this is stored into my database
        echo "<br>User ".$user[$i]." scan rate ".$scanrate[$i]." rank ".$key;
        
    }
}
 
Associate
Joined
30 Jul 2015
Posts
9
Have you not posted some code or is that all you have?

You've referenced $num_rows, $user, $scanrate but they are not assigned anywhere?

Also what errors are you seeing?
 
Back
Top Bottom