在某些条件下使用PHP排名

Maw*_* HL 3 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 
                     ) 
                ) 
      )
Run Code Online (Sandbox Code Playgroud)

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

$ 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 
            ) 
        ) 
Run Code Online (Sandbox Code Playgroud)

取出所有科目中的学生总得分后,我使用以下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);    
Run Code Online (Sandbox Code Playgroud)

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

Mát*_*osi 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()
 );
Run Code Online (Sandbox Code Playgroud)

有了这个,$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
        ) 
    ) 
Run Code Online (Sandbox Code Playgroud)

然后,在中setRankings,您可以检查是否包含$item["can_be_scored"]该项目并将其排除.