为学生分配职位,PHP

I am still a novice at PHP scripting.

I have an Array

$students = array(1201=>94,1203=>94,1200=>91, 1205=>89, 1209=>83, 1206=>65, 1202=>41, 1207=>38,1208=>37, 1204=>37,1210=>94);

From the associative array, the key are the student's exam no and the values are the student's scores. Then I used the 2 inbult PHP functions array_keys and array_values to separate the exam nos from the scores.

$exam_nos=(array_keys($students)); 
$marks=(array_values($students));

Then I passed the $marks array through the code below:

 $i=0;
$occurrences = array_count_values($marks);
$marks = array_unique($marks);
echo '<table border="1">';
foreach($marks as $grade) {
  if($grade == end($marks))$i += $occurrences[$grade]-1;
  echo str_repeat('<tr><td>'.$grade.': '.($i+1).'</td></tr>',$occurrences[$grade]);
  $i += $occurrences[$grade];
}
echo '</table><br />';

output:

 94: 1
 94: 1
 94: 1
 91: 4
 89: 5
 83: 6
 65: 7
 41: 8
 38: 9
 37: 11
 37: 11

And this is closer to what I want; to rank the scores such that if a tie is encountered, 1 or more positions are skipped, occurs at the end the position the items at the end are assigned a position equivalent toi the total number of ranked items. However, it would be much helpful if this could be done without separating the Array into 2 ... Questions: (1) I am pulling my hair how, from the $student array I could have something like:

Exam No Score Position
 1201     94      1
 1210     94      1
 1203     94      1
 1200     91      4
 1205     89      5
 1209     83      6
 1206     65      7
 1202     41      8
 1207     38      9
 1204     37      11
 1208     37      11

(2) I would like to be able to pick any student by exam no and be able to echo or print out her position e.g

the student 1207 is number 9.

I think I need to capture the postions in a variable, but how do I capture them? Well I don't know!

Could the experts help me here with a better way to achieve my 2 goals (please see questions 1 and 2)? I will try any suggestion that will help me disolve the 'metal blockage' I have hit.

If you're pulling out the students from a database (mentioned in the comments), you could retrieve them with the desired format directly using SQL.

However, I'm going to assume that that's not an option. You could do as follows:

$students = array(1201=>94,1203=>94,1200=>91, 1205=>89, 1209=>83, 1206=>65, 1202=>41, 1207=>38,1208=>37, 1204=>37,1210=>94);
arsort($students);// It orders high to low by value. You could avoid this with a simple ORDER BY clause in SQL.

$result = array();
$pos = $real_pos = 0;
$prev_score = -1;
foreach ($students as $exam_n => $score) {
    $real_pos += 1;// Natural position.
    $pos = ($prev_score != $score) ? $real_pos : $pos;// If I have same score, I have same position in ranking, otherwise, natural position.
    $result[$exam_n] = array(
                     "score" => $score, 
                     "position" => $pos, 
                     "exam_no" => $exam_n
                     );
    $prev_score = $score;// update last score.
}

$desired = 1207;
print_r($result);
echo "Student " . $result[$desired]["exam_no"] . ", position: " . $result[$desired]["position"] . " and score: ". $result[$desired]["score"];

Hope it helps you.

I would use a custom object to process the students individually and store them in an array.

$students = array(1201=>94,1203=>94,1200=>91, 1205=>89, 1209=>83, 1206=>65, 1202=>41, 1207=>38,1208=>37, 1204=>37,1210=>94);
arsort($students); // Sort the array so the higher scores are on top.

$newStudents = array();
$pos = 0;
$count = 0;
$holder = -1; // Assuming no negative scores.
foreach($students as $k=>$v){
    $count++; // increment real counter
    if($v < $holder || $holder == -1){
        $holder = $v;
        $pos = $count;
    }

    $newStudents[] = makeStudent($pos, $v, $k);
    // If you want the exam # as the array key.
    // $newStudents[$k] = $student;
}

$newStudents = fixLast($newStudents);

// outputs
print_r($newStudents);

foreach($newStudents as $v){
   echo "position : " . $v->position . "<br>";
   echo "score : " . $v->score . "<br>";
   echo "exam : " . $v->exam . "<br>";
}


function makeStudent($pos, $score,$examNo){
    $student = new stdClass(); // You could make a custom, but keeping it simple
    $student->position = $pos;
    $student->score = $score;
    $student->exam = $examNo;

    return $student;
}

function fixLast($students){
    $length = count($students) -1;
    $count = 0;
    $i = $length;
    while($students[$i]->position == $students[--$i]->position){
        $count++;
    }

    for($i = 0; $i <= $count; $i++){
        $students[$length - $i]->position = $students[$length - $i]->position + $count;
    }
    return $students;
}