姓名 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
应该怎么实现,请教各位大侠
18 个解决方案
#1
最好贴出你的数组。var_export();之后的。
#2
SELECT * FROM (SELECT @rank:=@rank+1 AS rank,a.* FROM `table` a,(SELECT @rank:=0)b ORDER BY a.score DESC)c或者用php实现 要简单一些
#3
排名多半在数组下标里了
或者遍历结果的时候,顺便添加一下。
或者遍历结果的时候,顺便添加一下。
#4
array (
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();之后的。
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();之后的。
#5
你打算按什么排序?
#6
我是想在数组里面添加一项
详细的请看一下问题
详细的请看一下问题
#7
$i=1;
$result = Array();
foreach($arr as $val){
$val['sort'] = $i;
$result[]=$val;
$i++;
}
不知道你要的是不是这样
#8
就是看了你的问题才会问你打算按什么排序,添加的名次放在哪里?
#9
添加的【排名】放到数组里面
#10
根本没必要……排序数据库完成了,
数组key+1就是排名了,不是吗?
#11
排序是按照【成绩】来排序
#12
如果2个成绩相同的话应该怎么处理啊
#13
那你是要按A-E的正序还是反序
#14
你这个应该有个学号?
#15
这个是问题的关键,循环输出的时候加一个判断就行了,成绩相同的相邻两数,排名后一个数跟前一个数相同,但是foreach的key一直保持不变就行了,问题就解决了。不必在数据库中另加一个字段来存储排名的。
#16
你运行一下面的测试就会明白了。
<?php
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;
}
?>
<?php
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;
}
?>
#17
问题已经解决谢谢大家
#18
问题已经解决
$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"];
}
}
}
$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"];
}
}
}
#1
最好贴出你的数组。var_export();之后的。
#2
SELECT * FROM (SELECT @rank:=@rank+1 AS rank,a.* FROM `table` a,(SELECT @rank:=0)b ORDER BY a.score DESC)c或者用php实现 要简单一些
#3
排名多半在数组下标里了
或者遍历结果的时候,顺便添加一下。
或者遍历结果的时候,顺便添加一下。
#4
array (
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();之后的。
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();之后的。
#5
你打算按什么排序?
#6
我是想在数组里面添加一项
详细的请看一下问题
详细的请看一下问题
#7
$i=1;
$result = Array();
foreach($arr as $val){
$val['sort'] = $i;
$result[]=$val;
$i++;
}
不知道你要的是不是这样
#8
就是看了你的问题才会问你打算按什么排序,添加的名次放在哪里?
#9
添加的【排名】放到数组里面
#10
根本没必要……排序数据库完成了,
数组key+1就是排名了,不是吗?
#11
排序是按照【成绩】来排序
#12
如果2个成绩相同的话应该怎么处理啊
#13
那你是要按A-E的正序还是反序
#14
你这个应该有个学号?
#15
这个是问题的关键,循环输出的时候加一个判断就行了,成绩相同的相邻两数,排名后一个数跟前一个数相同,但是foreach的key一直保持不变就行了,问题就解决了。不必在数据库中另加一个字段来存储排名的。
#16
你运行一下面的测试就会明白了。
<?php
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;
}
?>
<?php
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;
}
?>
#17
问题已经解决谢谢大家
#18
问题已经解决
$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"];
}
}
}
$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"];
}
}
}