<?php


$students=array();

array_push($students, array("name"=>"Jane doe", "class"=>"js1", "marks"=>20));
array_push($students, array("name"=>"Jhone Doe", "class"=>"js3", "marks"=>30));
array_push($students, array("name"=>"Kane doe", "class"=>"js2", "marks"=>50));
array_push($students, array("name"=>"Jane Kane", "class"=>"1s2", "marks"=>40));
array_push($students, array("name"=>"Jane Kane", "class"=>"1s2", "marks"=>40));
array_push($students, array("name"=>" Kane", "class"=>"1s2", "marks"=>40));
array_push($students, array("name"=>"Jakne", "class"=>"1s2", "marks"=>90));
array_push($students, array("name"=>"Jakne", "class"=>"1s2", "marks"=>70));
array_push($students, array("name"=>"Jakne", "class"=>"1s2", "marks"=>70)); 
array_push($students, array("name"=>"Jakne", "class"=>"1s2", "marks"=>70)); 
array_push($students, array("name"=>"Jakne", "class"=>"1s2", "marks"=>80));


usort($students, mySort('marks'));
function mySort($key)
{
    return function($a,$b) use ($key)
    {
        return strnatcmp($b[$key], $a[$key]);
    };
}



$all_marks=array();//
$other_almrk=array(0=>1);

// 
$dprevious=0;
foreach($students as $student)
{
    // $student['marks']
    // if($dprevious!=$dprevious)
    // {
    //     $position++;
    // }
    
    $dprevious=$student['marks'];//assigning new marks to previous marks  
    array_push($all_marks, $student['marks']); //push student marks to all_marks for existing counting
    
    $exist_count=0;// declear existance
    
    foreach($all_marks as $almrk)
    {
        // if previous marks is equal to the current mark, increse the existing count found
        if($almrk==$dprevious)
        {
            $exist_count++;
        }
        $other_almrk[$student['marks']]=$exist_count;// replace value of previous existing_count of  key(marks) with new existing_count with the same key(marks)
    }
}

echo json_encode($other_almrk);//For testing purpose

//Score Calculations 
$position=0;
$previousMark=0; 
foreach($students as $student)
{ 
    
    if($previousMark!=$student['marks'])
    {
        $position+=($other_almrk[$previousMark]);//Adding position based on prevoise marks exitance count in order to skip
    }
    $previousMark=$student['marks'];
      
    // echo '<div> '.$student['marks'].' POSITION '.$position.' Existed '.$other_almrk[$student['marks']].'</div>';
    echo '<div> '.$student['marks'].' POSITION '.$position.' </div>';
}
 
?>