有这样的一个数据
$arr = [
[
'id'=>1,
'score'=>10,
],
[
'id'=>2,
'score'=>30,
],
[
'id'=>3,
'score'=>50,
],
[
'id'=>4,
'score'=>50,
]
]
你想从小到大,取出前三名,如果第四名和第三名也相同,也取出来
先按score排序
array_multisort(array_column($arr,'score'),SORT_ASC,$arr);
前三名
$n = 3;
$int =3;
$results = array_slice($arr, 0, $n, true);
//前几名是否重复
while (count(array_count_values(array_column($results,'score')))
$results = array_slice($arr, 0, $int++, true);
}
然后前三名的最后一名和之后的比较
$length = count($arr);
$resultLength = count($results);
//$resultLength 比 $length小说明$arr 的还有元素,否则它们俩就相等了。然后比较key和key+1的值。
极限是所有的人都和第三名相同,自行判断是否需要所有人都胜出,来限制 $resultLength 的长度
while($resultLength
array_push($results,$arr[$resultLength]);
$resultLength = count($results);
}
最后如果要判断某一用户是否胜出
$ifWinner = 0;
if(in_array($id,array_column($results,'id'))){
$ifWinner = 1;
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
NOT IS BECAUSE I WANT TO WRITE,
BUT I WANT TO INCREASE,
SO I GO TO WRITE~~