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

标签 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 个)。
  • 找到剩余可用玩家最多的学校,该学校不是当前玩家迭代所在的学校。
  • 如果上述检查没有找到有任何玩家的学校,则求助于使用当前迭代玩家所在的同一所学校。这具有这样的效果,如果没有其他玩家留在同一学校,则同一学校的玩家可以互相玩分配池。
  • 从我们刚刚找到的学校分配一个任意玩家
  • 配对当前迭代的播放器和任意播放器
  • 将两名玩家从池中移除

在实现方面,我发现维护 2 个索引更容易 - 一个学校和 1 个玩家。我将它捆绑到几个类中,因为对象固有的引用性质使生活更轻松。我的代码在下面......它开箱即用,但可能需要一些调整。

<?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"
  }
}

关于PHP 以两组为一组对数组进行排序,避免合并具有相同值的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19387326/

相关文章:

PHP登录-使用SQL语句检测用户类型

php - JQuery 弹出窗口.....未捕获类型错误 : Object [object Object] has no method 'dialog'

php - 在 Apache + PHP + Bash 中让进程 fork 并独立于父进程

php - 将数组存储到数据库php

python - 在numpy中以维度的形式获取数字的重复

php - 将数组中的值放入带有键的二维数组中

在 C 中从 float 转换为 char[32](反之亦然)

java - 按字母升序对列表进行排序

php - 有没有更好的方法来设置来自 php 脚本中具有相似值的表单的两个输入字段的四个不同变量

windows - 使用具有空值的命令按三列对 csv 文件进行排序?