我现在有从DB检索出来的一个数组里面有如下内容姓名 A B C D E成绩 80 70 90 80 60 按照成绩排序过后姓名 C A D B E
成绩 90 80 80 70 60我现在想往数组里面添加一项 [排名]
实现以下效果排名 1 2 2 4 5
姓名 C A D B E
成绩 90 80 80 70 60也可以颠倒过来
排名 5 4 2 2 1
姓名 E B D A C
成绩 60 70 80 80 90应该怎么实现,请教各位大侠
成绩 90 80 80 70 60我现在想往数组里面添加一项 [排名]
实现以下效果排名 1 2 2 4 5
姓名 C A D B E
成绩 90 80 80 70 60也可以颠倒过来
排名 5 4 2 2 1
姓名 E B D A C
成绩 60 70 80 80 90应该怎么实现,请教各位大侠
0 =>
array (
'UserKihonnInfo' =>
array (
'id' => '10',
'user_id' => 'A010',
'user_name' => '张三',
),
'Gameresume' =>
array (
'total_point' => '85',
),
'A1' =>
array (
'total_point' => NULL,
),
'A2' =>
array (
'total_point' => NULL,
),
'A3' =>
array (
'total_point' => '170',
),
),
1 =>
array (
'UserKihonnInfo' =>
array (
'id' => '5',
'user_id' => 'A005',
'user_name' => '李四',
),
'Gameresume' =>
array (
'total_point' => '80',
),
'A1' =>
array (
'total_point' => '160',
),
'A2' =>
array (
'total_point' => '160',
),
'A3' =>
array (
'total_point' => '160',
),
),
2 =>
array (
'UserKihonnInfo' =>
array (
'id' => '9',
'user_id' => 'A009',
'user_name' => '小王',
),
'Gameresume' =>
array (
'total_point' => '80',
),
'A1' =>
array (
'total_point' => NULL,
),
'A2' =>
array (
'total_point' => NULL,
),
'A3' =>
array (
'total_point' => '160',
),
),
3 =>
array (
'UserKihonnInfo' =>
array (
'id' => '7',
'user_id' => 'A007',
'user_name' => '小赵',
),
'Gameresume' =>
array (
'total_point' => '60',
),
'A1' =>
array (
'total_point' => '120',
),
'A2' =>
array (
'total_point' => '120',
),
'A3' =>
array (
'total_point' => '120',
),
),
4 =>
array (
'UserKihonnInfo' =>
array (
'id' => '8',
'user_id' => 'A008',
'user_name' => '作家',
),
'Gameresume' =>
array (
'total_point' => '50',
),
'A1' =>
array (
'total_point' => '100',
),
'A2' =>
array (
'total_point' => '100',
),
'A3' =>
array (
'total_point' => '100',
),
),
)
这个是var_export();之后的。
$i=1;
$result = Array();
foreach($arr as $val){
$val['sort'] = $i;
$result[]=$val;
$i++;
}不知道你要的是不是这样
根本没必要……排序数据库完成了,数组key+1就是排名了,不是吗?
session_start();
echo'排名'.' '.'分数'.'<br />';
$result = Array(100,99,98,98,97,97,96,95,95,95,95,94,93);//已排序的成绩数组
for($i=0;$i<count($result);$i++){
$pm=$SESSION["pm"];
if(empty($pm)){
$pm=$i;
}
$pm=$i;
if($result[$i]==$result[($i-1)]){
$pm=$SESSION["pm"];
}else{
$pm=$pm+1;
}
switch(strlen($pm)){
case 1:
$space='  ';
break;
case 2:
$space=' ';
break;
}
echo ' '.$pm.$space.$result[$i].'<br />';
$SESSION["pm"]=$pm;
}
?>
$resultList = $this->UserInfo->getUserSearch();foreach( $resultList as $k=>$v){
if($resultList[$k]["A1"]["total_point"] == "" ) {
$resultList[$k]["A1"]["total_point"] = 0;
}
if($resultList[$k]["A2"]["total_point"] == "" ) {
$resultList[$k]["A2"]["total_point"] = 0;
}
if($resultList[$k]["A3"]["total_point"] == "" ) {
$resultList[$k]["A3"]["total_point"] = 0;
}
$resultList[$k]["UserHistory"]["ping"]= ($k+1)."位";
if ($k > 0){
$lk = $k - 1;
if ($resultList[$k]["A"]["user_point"] == $resultList[$lk]["A"]["user_point"]){
$resultList[$k]["UserHistory"]["ping"] = $resultList[$lk]["UserHistory"]["ping"];
}
} }