PHP以两个为一组排序数组,避免合并具有相同值的项目

时间:2013-10-15 17:27:09

标签: php arrays sorting

我有一个代码来为锦标赛生成支架表。我有球员,每个球员都与学校有联系。我需要与玩家排序一个阵列,这样就不会有同一所学校的球员的第一场比赛(或者可能是同一所学校的比赛)。

这样的事情:

$players = array( 
             array('name' => 'juan', 'school' => 'ABC'),   // 0
             array('name' => 'leo', 'school' => 'ABC'),    // 1
             array('name' => 'arnold', 'school' => 'DEF'), // 2
             array('name' => 'simon', 'school' => 'DEF'),  // 3
             array('name' => 'luke', 'school' => 'ECD'),   // 4
             array('name' => 'ash', 'school' => 'ECD'),    // 5
           );
// code to sort here

array_chunk($players, 2); // this generate an array with groups of two for matches.

在上面的例子[0]和[1]中,因为他们在同一所学校,所以不能一起去。例如,[0]可以使用3。

我正在尝试使用usort,但我不确定采用这种方式的正确方法。

2 个答案:

答案 0 :(得分:1)

好的,一个新的答案重新审视了这个问题。我想这个算法应该是这样的:

  • 对未分配的玩家进行迭代(注意此列表一次减少2个)。
  • 找到学校中剩余玩家最多的学校,而不是当前玩家所在的学校。
  • 如果上述检查未找到有任何球员的学校,则使用与当前球员迭代相同的学校。这样可以确保同一所学校的球员可以在没有其他球员留在分配池。
  • 从我们刚刚找到的学校分配一名任意球员
  • 配对当前迭代的播放器和任意播放器
  • 从泳池中移除两名玩家

实施方面,我发现维护2个索引更容易 - 一个是学校,一个是玩家。我把它捆绑成了几个类,因为对象固有的引用性使得生活更容易。我的代码如下......它开箱即用,但可能需要一些调整。

<?php

class School {
        protected $name;
        protected $players = [];

        public function __construct($name) {
                $this->name = $name;
        }

        public function get_name() {
                return $this->name;
        }

        public function add_player($name) {
                $this->players[] = $name;
        }

        public function del_player($name) {
                if (($index = array_search($name, $this->players)) !== false) {
                        unset($this->players[$index]);
                }
        }

        public function player_count() {
                return count($this->players);
        }

        public function get_player() {
                if (!reset($this->players)) {
                        return false;
                }

                return [
                        'school' => $this->name,
                        'name' => reset($this->players),
                ];
        }
}

class Players {
        protected $schools_index = [];
        protected $player_index = [];

        public function add_player($school, $player) {
                // Create school if not exists
                if (!isset($this->schools_index[$school])) {
                        $this->schools_index[$school] = new School($school);
                }

                // Add player to school and own index
                $this->schools_index[$school]->add_player($player);
                $this->player_index[$player] = $school;
        }

        public function del_player($school, $player) {
                // From school index
                $this->schools_index[$school]->del_player($player);

                // From own index
                if (isset($this->player_index[$player])) {
                        unset($this->player_index[$player]);
                }
        }

        public function biggest_school($exclude = null) {
                $rtn = null;

                // Find school excluding the exclude. Don't get schools with nobody left in them.
                foreach ($this->schools_index as $name=>$school) {
                        if ((!$exclude || $name != $exclude) && ($school->player_count()) && (!$rtn || $rtn->player_count() < $school->player_count())) {
                                $rtn = $school;
                        }
                }

                // If we didn't get a school, shitcan the exclude and try the excluded school
                if (!$rtn && $exclude) {
                        if ($this->schools_index[$exclude]->player_count()) {
                                $rtn = $this->schools_index[$exclude];
                        }
                }

                return $rtn;
        }

        public function get_player() {
                if (!reset($this->player_index)) {
                        return false;
                }

                return [
                        'school' => reset($this->player_index),
                        'name' => key($this->player_index),
                ];
        }

        public static function from_players_arr(array $players) {
                $obj = new static();

                foreach ($players as $player) {
                        // Add to indexes
                        $obj->add_player($player['school'], $player['name']);
                }

                return $obj;
        }
}

$players = array(
        array('name' => 'juan', 'school' => 'ABC'),
        array('name' => 'leo', 'school' => 'ABC'),
        array('name' => 'arnold', 'school' => 'ABC'),
        array('name' => 'simon', 'school' => 'ABC'),
        array('name' => 'luke', 'school' => 'ABC'),
        array('name' => 'alan', 'school' => 'JKL'),
        array('name' => 'jeff', 'school' => 'BAR'),
        array('name' => 'paul', 'school' => 'FOO'),
);

$players_obj = Players::from_players_arr($players);

$pairs = [];

while ($player = $players_obj->get_player()) {
        $players_obj->del_player($player['school'], $player['name']);

        $opponent = $players_obj->biggest_school($player['school'])->get_player();

        $pairs[] = [
                $player['name'],
                $opponent['name'],
        ];

        $players_obj->del_player($opponent['school'], $opponent['name']);
}

var_dump($pairs);

输出如下:

array(4) {
  [0] =>
  array(2) {
    [0] =>
    string(4) "juan"
    [1] =>
    string(4) "alan"
  }
  [1] =>
  array(2) {
    [0] =>
    string(3) "leo"
    [1] =>
    string(4) "jeff"
  }
  [2] =>
  array(2) {
    [0] =>
    string(6) "arnold"
    [1] =>
    string(4) "paul"
  }
  [3] =>
  array(2) {
    [0] =>
    string(5) "simon"
    [1] =>
    string(4) "luke"
  }
}

答案 1 :(得分:0)

我们要做的是将球员分为两组:竞争者1&amp;竞争者2.我们将与同一所学校的连续球员填补竞争者:

如果我们为了简洁而将学校命名为单一字母,我们基本上就是:

 A A A A B B B C
 C C D D E E F F

或者,如果我们翻转它,它会变得更清晰:

 A C
 A C
 A D
 A D
 B E
 B E
 B F
 C F

如果一所学校有&gt;球员总数的一半?好吧,让我们看看:

 A A A A A
 A B B C D

所以:

 A A <= one A vs. A, which is unavoidable, but the method still works.
 A B
 A B
 A C
 A D

我在这里作了一点作弊:我先从最大的学校挑选出来的球员。然而,只要我们将学校聚集在一起,它仍然有效。让我们得出$a = range('A','F'); shuffle($a):的输出,其结果是:FCADBE,它给了我们:

 F A
 F D
 C D
 C B
 C B
 A B
 A E
 A E

..适用于A&gt;半:

 C A
 A A <= that one double again, unavoidable
 A D
 A B
 A B

让我们把它分成几部分。我们要做的是:

  1. 按学校排序球员
  2. 将此已排序的数组分成两组。
  3. 通过从第一组&amp;添加来创建对第二组中的一个,按顺序排列。
  4. 减少它的好处是你可以找到答案:

    1. hundreds of questions on SO
    2. this is a nice answer
    3. 我们可以使用MultiIterator,这将是一个逻辑(和工作)选择,但我将向您展示另一种创建2个数组对的简短方法(如果您有3个数组,则为三元组等)。 )
    4. 所以,让我们这样做:

       //sort by school
       $players = ... your array ...
       usort($players,function($playerA, $playerB){
           return strcmp($playerA['school'], $playerB['school']);
       });
       //voila, sorted
      
       //break this into 2 groups:
       $size = ceil(count($players) / 2); // round UP
       $groupA = array_slice($players,0,$size);
       $groupB = array_slice($players,$size);
      
       //create our duels array:
       $duels = array_map(null, $groupA, $groupB);
      

      $duels现在输入了以下内容:

      [
          [
              {
                  "name": "juan",
                  "school": "ABC"
              },
              {
                  "name": "arnold",
                  "school": "DEF"
              }
          ],
          [
              {
                  "name": "leo",
                  "school": "ABC"
              },
              {
                  "name": "ash",
                  "school": "ECD"
              }
          ],
          [
              {
                  "name": "simon",
                  "school": "DEF"
              },
              {
                  "name": "luke",
                  "school": "ECD"
              }
          ]
      ]