Php 如何对数组排序并保留结果,然后分配值
我有这个问题,我不知道如何给同一位置的球队打分。例如,3支球队的得分率相近,排名第三,因此他们每个人的得分都相近Php 如何对数组排序并保留结果,然后分配值,php,arrays,sorting,Php,Arrays,Sorting,我有这个问题,我不知道如何给同一位置的球队打分。例如,3支球队的得分率相近,排名第三,因此他们每个人的得分都相近 如果球队是第一名,他们会得到10分 如果球队排名第二,他们将获得9分 如果球队排名第三,他们将获得8分 如果球队排名第四,他们得7分 如果球队排名第五,他们将获得6分 如果球队排名第六,他们将得到5分 如果球队排名第七,他们将获得4分 如果球队排名第八,他们将获得3分 假设我有8支队伍 $team_aScore and $team_cScore = 98%; (first pla
- 如果球队是第一名,他们会得到10分
- 如果球队排名第二,他们将获得9分
- 如果球队排名第三,他们将获得8分
- 如果球队排名第四,他们得7分李>
- 如果球队排名第五,他们将获得6分
- 如果球队排名第六,他们将得到5分李>
- 如果球队排名第七,他们将获得4分李>
- 如果球队排名第八,他们将获得3分
$team_aScore and $team_cScore = 98%; (first place)
$team_dScore, $team_eScore and $team_fScore = 96%; (3rd place)
$team_bScore = 94%; (5th place)
$team_gScore = 97%; (2nd place)
$team_hScore = 95%; (4th place)
我所做的是首先以最高的分数对球队进行排序。我知道a队的id为1,b队的id为2,以此类推
$array = array(1=>$team_aScore, 2=>$team_bScore, 3=>$team_cScore, 4=>$team_dScore, 5=>$team_eScore, 6=>$team_fScore, 7=>$team_gScore, 8=>$team_hScore);
arsort($array);
$x = 0;
foreach ($array as $key => $val) {
$x++;
if($x==1) {$givepts = 10;}
if($x==2) {$givepts = 9;}
if($x==3) {$givepts = 8;}
if($x==4) {$givepts = 7;}
if($x==5) {$givepts = 6;}
if($x==6) {$givepts = 5;}
if($x==7) {$givepts = 4;}
if($x==8) {$givepts = 3;}
$q = mysql_query("INSERT INTO teamScore(id,score) VALUES($x,$givepts)");
}
结果就是这样,每个队都得到了个人的分数
- a队=10分
- 小组c=9分
- 团队=8分
- 小组d=7分
- 团队e=6分
- 团队f=5件
- 团队h=4个
- 团队b=3pts
- a队=10分
- 小组c=10分
- 团队=9分
- 团队d=8分
- 团队e=8分
- 团队f=8分
- 团队h=7分
- b队=6分
team\u score
函数,为您的团队进行排序和分数分配。我将初始赋值点设置为10
。然后,我将最后一次检查的分数存储在一个变量中,并对照它检查当前分数。如果相同,我不会减少赋值点的值
<?php
// Data Setup
$team_tags = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h');
$team_scores = array(98, 98, 97, 96, 95, 95, 95, 94);
shuffle($team_scores);
$teams = array();
foreach ($team_tags as $tag) {
$teams[$tag] = array_pop($team_scores);
}
// Sort and Score
$team_points = team_score($teams);
var_dump($team_points);
var_dump($teams);
function team_score($teams) {
// Sort
arsort($teams);
// Max Points
$points = 10;
$last_score = false;
// Assign Points
$team_points = array();
foreach ($teams as $tag => $score) {
if ($last_score && $score < $last_score) {
$points--;
}
$team_points[$tag] = $points;
$last_score = $score;
}
return $team_points;
}
谢谢,但问题是目前没有给出百分比。因此,它应该有另一个函数来排序哪个团队首先获得最高的百分比,或者哪个团队具有相似的百分比,这样您就可以将值放入$team_scores=array($x)函数team_scores
是按百分比排序的。在//数据设置
下的区域中。我只是设置你说你已经拥有的数据。此外,如前所述,我改变了数据的组织方式,使其可行。以上代码中提供了百分比(分数)和分数。我正在var\u dump
为您查看这两个文件。您是否单独在文件中运行此代码?看看我的意思:这些答案对你有帮助吗?如果没有,请让我们知道,以便我们可以提供更多帮助。如果是,请您将最有用的答案标记为“已接受”好吗?谢谢