【问题标题】:PHP Sort array joint positionsPHP排序数组关节位置
【发布时间】:2018-01-22 10:22:50
【问题描述】:

我有一组玩家,每个玩家都有一些分数。

我可以使用自定义的 usort 函数轻松地按点的顺序对数组进行排序。

但是当两个玩家的积分相同时,我需要将他们列为列表中相同的获胜位置。

例如

  • 尼克 - 25
  • 汤姆 - 18
  • 克里斯 - 18
  • 戴夫 - 16
  • 詹姆斯 - 8

在这种情况下,我需要的数据是

  • 尼克 - 第一
  • 汤姆 - (联合)第二
  • 克里斯 - (并列)第二
  • 戴夫第三
  • 詹姆斯四世

因此得分相同的玩家被指定为相同的排名位置。

计算这些位置的最佳方法是什么?

谢谢!

【问题讨论】:

  • 您可以使用['1' => ['someone'], '2' => ['oneguy','another guy'] ]等关联数组将其作为键值来解决
  • 你试过什么?你当前的代码是什么样子的?

标签: php arrays


【解决方案1】:

这将为您提供您想要的附加修复:如果您有两名球员在位置 2,下一个球员应该在位置 4。如果您不希望这个额外的修复将 $count++; 移动到 if 语句中。

$count = 0;
$position = 0;
$last_score = -1;

foreach ($players as $player)
{
    $count++;

    if ($last_score !== $player->score)
    {
        $position = $count;
    }

    $player->position = $position;
    $last_score = $player->score;
}

【讨论】:

  • 迄今为止完美且最简单的解决方案。非常感谢!!
【解决方案2】:

我认为以下代码可以按要求完成工作:

$scores = array('Nick'=>25,'Tom'=>18,'Chris'=>18,'Dave'=>16, 'James'=> 8,);

$scores_numbers = array_unique(array_values($scores)); 
$scores_counter = array_count_values($scores);
$pos = 1;
foreach($scores_numbers as $num) {
    foreach($scores as $name=>$score) {
      if($num == $score) {
        if($scores_counter[$score] > 1) {
          echo  "$name - (joint) $pos<br />";
        } else {
          echo  "$name - $pos<br />";
        }
      }
    }
  $pos++;
}

我已更新代码以避免重复输出。

【讨论】:

    【解决方案3】:

    如果您要从数据库中检索玩家统计数据,那么下面的代码可能有助于对它们进行排名:

    <?php
    
    $players = [
        ['name' => 'Ash',
        'point' => 0
        ],
        ['name' => 'Bob',
         'point' => 10
        ],
        ['name' => 'Cane',
        'point' => 0
        ],
        ['name' => 'Dory',
        'point' => 6
        ],
        ['name' => 'Efat',
        'point' => 6
        ],
      ];
    
    usort($players, function($v1, $v2){
        return $v2['point'] - $v1['point'];
    });
    
    $ranking = [];
    
    $currentPosition = 0;
    $heighestPoint = PHP_INT_MIN;
    
    foreach($players as $player){
        if($heighestPoint !== $player['point']){
            $currentPosition++;
            $heighestPoint = $player['point'];
        }
    
            $ranking[$currentPosition][] = $player;
    }
    
    print_r($ranking);
    

    输出:

    Array
    (
    [1] => Array
        (
            [0] => Array
                (
                    [name] => Bob
                    [point] => 10
                )
    
        )
    
    [2] => Array
        (
            [0] => Array
                (
                    [name] => Dory
                    [point] => 6
                )
    
            [1] => Array
                (
                    [name] => Efat
                    [point] => 6
                )
    
        )
    
    [3] => Array
        (
            [0] => Array
                (
                    [name] => Ash
                    [point] => 0
                )
    
            [1] => Array
                (
                    [name] => Cane
                    [point] => 0
                )
    
        )
    
     )
    

    【讨论】:

      【解决方案4】:

      希望对你有帮助

      /*Below array must be sorted  */
      $a1=array("Nick"=>25,"Tom"=>18,"Chris"=>18,"Dave"=>16,"James"=>8); 
      
      $makeUnique=array_unique($a1);
      $makeUnique=array_values($makeUnique);
      
      $pArr=array_map('setPosition',$a1);
      
      print_r($pArr);
      
      
      function setPosition($a){
          Global $makeUnique;
          return array_search($a,$makeUnique)+1;
      }    
      

      输出

      数组([尼克] => 1 [汤姆] => 2 [克里斯] => 2 [戴夫] => 3 [詹姆斯] => 4)

      【讨论】:

        【解决方案5】:

        我使用usort 对关联数组进行排序

        $players = [["name" => "Nick" , "score" => 25] , ["name" => "Tom" , "score" => 18] , ["name" => "chris" , "score" => 18] , ["name" => "dave" , "score" => 16 ] , ["name" => "james" , "score" => 8]];
        
        usort($players , function($a , $b) {return $b["score"] - $a["score"];} );
        
        var_dump($players);
        
        $lastScore = null;
        $pos = 1;
        foreach($players as $player)
        {
            if($lastScore != null && $lastScore == $player["score"])
                echo $pos-1 . " - " . $player["name"] . '<br/>';
            else
                echo $pos++ . " - " . $player["name"] . '<br/>';
        
            $lastScore = $player["score"];
        }
        

        teh playground签出此代码

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-26
          • 2011-01-28
          • 1970-01-01
          • 2012-08-25
          • 1970-01-01
          • 2012-03-07
          相关资源
          最近更新 更多