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

时间:2018-09-29 19:02:41

标签: php php-7 php-7.1

我无法从免费的在线测试网站上弄清楚该问题的解决方法。这是链接:https://www.testdome.com/questions/php/league-table/19939?questionIds=7278,19939&generatorId=30&type=fromtest&testDifficulty=Hard

但是,更清楚地说,我也在写问题和答案。上面的链接中提供了起始答案。

问题:

  

LeagueTable类跟踪联盟中每个球员的得分。每次游戏后,玩家都使用recordResult函数记录其得分。

     

使用以下逻辑计算联盟中的球员排名:

     

得分最高的玩家排名第一(排名第一)。得分最低的玩家排名最后。   如果两个玩家的得分并列,那么玩过最少游戏的玩家将排名更高。   如果两名球员在得分和打牌次数上并列,那么在球员名单上第一名的球员排名更高。   实现playerRank函数,以返回给定等级的玩家。

     

例如:

$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);
  

所有玩家得分均相同。但是,阿诺德(Arnold)和克里斯(Chris)的比赛次数少于迈克(Mike),而且由于克里斯(Chris)在球员名单中早于阿诺德(Arnold),因此他排名第一。因此,上面的代码应显示“ Chris”。

我的代码:

<?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);

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

2 个答案:

答案 0 :(得分:1)

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

<?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 :(得分:0)

在您的情况下使用usort

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);