我在 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代码计算并列排名:
$总分:
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 分),其他保持不变。 请帮忙。谢谢。
最佳答案
当您处理$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"]
如果为假则排除该项目。
关于php - 在特定条件下使用 PHP 进行排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44986460/