dpy33121 2015-02-07 17:58
浏览 10
已采纳

为学生分配职位,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.

  • 写回答

2条回答 默认 最新

  • dongse5528 2015-02-07 18:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch