在某些条件下使用PHP排名

时间:2017-07-08 12:55:21

标签: php arrays sorting ranking

我有几个学生在php数组中得分。

$ firstarray:

Array ( 
       [0] => Array ( 
             [6] => Array ( //I want to skip this student from ranking
                     [ENGLISH] => 45.00 
                     [MATHEMATICS] => 5.00 
                     [SCIENCE] => 40.00 
                    ) 
                ) 
      [1] => Array ( 
             [7] => Array ( 
                     [ENGLISH] => 41.00 
                     [MATHEMATICS] => 40.00 
                     [SCIENCE] => 47.00 
                     ) 
                ) 
      ) 
      [2] => Array ( 
             [8] => Array ( 
                     [ENGLISH] => 42.00 
                     [MATHEMATICS] => 44.00 
                     [SCIENCE] => 40.00 
                     ) 
                )
      [3] => Array ( 
             [9] => Array ( 
                     [ENGLISH] => 42.00 
                     [MATHEMATICS] => 25.00 
                     [SCIENCE] => 31.00 
                     ) 
                ) 
      )

在另一个数组中,我有每个学生得分的总得分。:

$ secondarray:

Array ( [7] => 
           Array ( 
             [score] => 128 
             [rank] => 1 
            ) 
        [8] => 
           Array ( 
             [score] => 126
             [rank] => 2 
            ) 
        [9] => 
           Array ( 
             [score] => 98
             [rank] => 3 
            ) 
       [6] => 
           Array ( 
             [score] => 90 
             [rank] => 4 
            ) 
        ) 

在取出所有科目中的学生总得分后,我使用以下PHP代码来计算得分的排名:

$ totalmarkscore:

   Array ( 
       [0] => Array ( 
            [6] => 90 
           ) 
       [1] => Array ( 
            [7] => 128 
           ) 
       [2] => Array ( 
            [8] => 126 
           )
       [3] => Array ( 
            [9] => 98
           ) 
       ) 

   $rankchu =array();
   foreach($totalmarkscore as $k => $vl){
      if(is_array($vl)){
          foreach($vl as $hlutna =>$ken){
             $rankchu[$hlutna]= $ken;
      }
    }
  }
  $secondarray = setRankings($rankchu);    

我的问题是:我如何跳过计算每个科目没有得到最少15分的学生的等级?但是我仍然希望向学生展示得分细节,我只是想从排名中跳过它然后保持排名顺序。在上面的例子中,我想跳过id = 6的学生,因为他没有从排名计算得到数学中的最小15分(他只得到5分),其他东西保持不变。 请帮忙。感谢。

1 个答案:

答案 0 :(得分:5)

当你处理$firstarray时,你需要保留任何分数是否低于15的信息。这里我们添加一个can_be_scored标志来存储:

 $totalmarkscore = array_reduce(
   $firstarray,
   function($result, $item) {
     $id = key($item);
     $scores = $item[$id];
     $result[$id] = array(
       "score" => array_sum($scores),
       "can_be_scored" => min($scores) >= 15
     );
     return $result;
   },
   array()
 );

有了这个,$totalmarkscore应该是这样的:

Array (
    [7] => 
       Array ( 
         [score] => 128 
         [can_be_scored] => true
        ) 
    [8] => 
       Array ( 
         [score] => 126
         [can_be_scored] => true
        ) 
    [9] => 
       Array ( 
         [score] => 98
         [can_be_scored] => true
        ) 
   [6] => 
       Array ( 
         [score] => 90 
         [can_be_scored] => false
        ) 
    ) 

然后,在setRankings中,您可以检查是否$item["can_be_scored"]并排除该项目是否为假。