首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP中带有条件语句的二维关联数组排序

PHP中带有条件语句的二维关联数组排序
EN

Stack Overflow用户
提问于 2018-09-29 19:02:41
回答 4查看 1.4K关注 0票数 1

我很难弄清楚如何解决这个问题,这是一个免费的在线考试网站。下面是链接:https://www.testdome.com/questions/php/league-table/19939?questionIds=7278,19939&generatorId=30&type=fromtest&testDifficulty=Hard

但更清楚的是,我也在写问题和答案。开始的答案在上面写的链接中。

问题:

LeagueTable类跟踪一个联盟中每个球员的得分。每场比赛结束后,玩家用recordResult函数记录他们的分数。 球员在联盟中的排名是按照以下逻辑计算的: 得分最高的选手排名第一(第一名)。得分最低的选手排在最后。如果两名球员在得分上打成平局,那么打过最少比赛的球员就会排得更高。如果两名球员在得分和比赛次数上打成平局,那么排在第一位的球员就会排得更高。实现返回给定级别的播放器的playerRank函数。 例如:

代码语言:javascript
复制
$table = new LeagueTable(array('Mike', 'Chris', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 3);
$table->recordResult('Arnold', 5);
$table->recordResult('Chris', 5);
echo $table->playerRank(1);

所有的球员都有相同的分数。然而,阿诺德和克里斯的上场次数比迈克少,因为克里斯在阿诺德之前的球员名单上,他排在第一位。因此,上面的代码应该显示"Chris“。

我的代码:

代码语言:javascript
复制
<?php

class LeagueTable
{
    public function __construct($players)
    {
        $this->standings = array();
        foreach($players as $index => $p)
        {
            $this->standings[$p] = array
            (
                'index' => $index,
                'games_played' => 0, 
                'score' => 0
            );
        }
    }

    public function recordResult($player, $score)
    {
        $this->standings[$player]['games_played']++;
        $this->standings[$player]['score'] += $score;
    }

    public function playerRank($rank)
    {
        // I'm not sure what to do in here, not even sure where to place the conditional statements
        // but here's me trying to figure it out which I'm 90% sure I'm doing it wrong, 
        // since I'm using too many foreach function and arrays. Most probably not even close
        // to the correct answer.

        $comparison = $result = $this->standings;
        $player_names = array();

        foreach($this->standings as $name => $records)
        {
            foreach($comparison as $name_compare => $records_compare)
            {     
                if($this->standings[$name]['score'] > $comparison[$name_compare]['score'])
                {
                    $result[$name]['index'] = $this->standings[$name]['index'];
                }
                else if($this->standings[$name]['score'] == $comparison[$name_compare]['score']
                      && $this->standings[$name]['games_played'] < $comparison[$name_compare]['games_played'])
                {
                    $result[$name]['index'] = $this->standings[$name]['index'];
                }
                else if($this->standings[$name]['score'] == $comparison[$name_compare]['score']
                        && $this->standings[$name]['games_played'] == $comparison[$name_compare]['games_played'])
                {
                    $result[$name]['index'] = $this->standings[$name]['index'];
                }

                // This is where I'm confused, although there are conditional statemens there
                // but the code inside each "if" and "else if" is the same.

            }
        }

        foreach($result as $name => $records)
        {
            array_push($player_names,$name);
        }

        return $player_names[$rank-1]; //This should return "Chris" based on the record result, but it's not
    }
}

$table = new LeagueTable(array('Mike', 'Chris', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 6);
$table->recordResult('Arnold', 5);
$table->recordResult('Chris', 5);
echo $table->playerRank(1);

有人能帮我解决这个问题吗?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2018-09-29 20:04:36

在你的案例中使用usort

代码语言:javascript
复制
usort($this->standings, function($a, $b) {
  // Compare scores
  $r = $b['score'] - $a['score'];
  // If two players are tied on score, 
  // then the player who has played the fewest games is ranked higher
  if(! $r) {
    $r = $a['games_played'] - $b['games_played'];
  }
  // If two players are tied on score and number of games played, 
  // then the player who was first in the list of players is ranked higher
  if(! $r) {
    $r = $a['index'] - $b['index'];
  }
  return $r;        
});

// You can watch result of sorting
print_r($this->standings);
票数 2
EN

Stack Overflow用户

发布于 2020-01-15 08:41:39

下面你可以找到完整的答案

代码语言:javascript
复制
<?php
class LeagueTable
{
    public function __construct($players)
    {
        $this->standings = array();
        foreach($players as $index => $p)
        {
            $this->standings[$p] = array
            (
                'index' => $index,
                'games_played' => 0, 
                'score' => 0,
                'rank' => $index
            );
        }
    }

    public function recordResult($player, $score)
    {
        $this->standings[$player]['games_played']++;
        $this->standings[$player]['score'] += $score;
    }

    function swap(&$x,&$y) {
        $tmp=$x;
        $x=$y;
        $y=$tmp;
    }  

    public function reRank()
    {
       //ranking all according to score
        foreach($this->standings as $player => $records) {

          foreach($this->standings as $player1 => $records1) {

            if (($records['score'] > $records1['score']) && ($records['rank'] >= $records1['rank']))
            {
                $this->swap($this->standings[$player]['rank'],$this->standings[$player1]['rank']);
            }

            // according to game played
            if (($records['score'] == $records1['score']) && ($records['games_played'] > $records1['games_played']))
            {
                $this->swap($this->standings[$player]['rank'],$this->standings[$player1]['rank']);
            }

            // according to index
            if (($records['score'] == $records1['score']) && ($records['games_played'] == $records1['games_played'])&&
            ($records['index'] > $records1['index']))
            {
                $this->swap($this->standings[$player]['rank'],$this->standings[$player1]['rank']);
            }
          }
       }
    }

    public function playerRank($rank)
    {
        $this->reRank();
        foreach($this->standings as $player => $records)
        {
            if ($records['rank']==$rank-1)
            return $player;
        }
    }
}

$table = new LeagueTable(array('Chris', 'Mike', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 3);
$table->recordResult('Arnold', 5);
$table->recordResult('Chris', 5);

echo $table->playerRank(1);
?>
票数 1
EN

Stack Overflow用户

发布于 2021-11-16 19:01:38

请试试下面的代码。看起来不错。

代码语言:javascript
复制
<?php
class LeagueTable
{
    public function __construct($players)
    {
        $this->standings = array();
        foreach($players as $index => $p)
        {
            $this->standings[$p] = array
            (
                'index' => $index,
                'games_played' => 0, 
                'score' => 0,
                'name' => $p
            );
        }
    }

    public function recordResult($player, $score)
    {
        $this->standings[$player]['games_played']++;
        $this->standings[$player]['score'] += $score;
    }

    public function playerRank($rank)
    {

        $comparison = $result = $this->standings;
        $player_names = array();
       
       usort($this->standings, "comparatorFunc");
       

        foreach($this->standings as $name => $records)
        {
            array_push($player_names,$records['name']);
        }
        // echo "hello<br>";
        // print_r($player_names);
        return $player_names[$rank-1]; 
    }
}


function comparatorFunc($a, $b) {
    # Compare scores
    $r = $b['score'] - $a['score'];
    # If two players are tied on score, 
    # then the player who has played the fewest games is ranked higher
    if(! $r) {
        $r = $a['games_played'] - $b['games_played'];
    }
    # If two players are tied on score and number of games played, 
    # then the player who was first in the list of players is ranked higher
    if(! $r) {
        $r = $a['index'] - $b['index'];
    }
    return $r;        
}
    
$table = new LeagueTable(array('Mike', 'Chris', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 6);
$table->recordResult('Arnold', 2);
$table->recordResult('Arnold', 3);
$table->recordResult('Chris', 5);
echo $table->playerRank(1);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52571584

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档