PHP以两个为一组排序数组,避免合并具有相同值的项
我有一个代码来为比赛生成括号表。我有球员,每个球员都与一所学校有关。我需要将一个数组与玩家进行排序,这样就不会有同一学校的玩家的第一场比赛(或者可能是同一学校的较低级别的比赛) 大概是这样的:PHP以两个为一组排序数组,避免合并具有相同值的项,php,arrays,sorting,Php,Arrays,Sorting,我有一个代码来为比赛生成括号表。我有球员,每个球员都与一所学校有关。我需要将一个数组与玩家进行排序,这样就不会有同一学校的玩家的第一场比赛(或者可能是同一学校的较低级别的比赛) 大概是这样的: $players = array( array('name' => 'juan', 'school' => 'ABC'), // 0 array('name' => 'leo', 'school' => 'ABC'),
$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个)
- 找到剩余玩家最多的学校,而不是当前玩家迭代所在的学校
- 如果上面的检查没有找到任何玩家所在的学校,请使用与当前玩家迭代相同的学校。这样做的效果是,如果没有其他玩家留在分配池中,同一学校的玩家可以互相玩
- 从我们刚找到的学校分配一个任意玩家
- 将当前迭代的播放器与任意播放器配对
- 将两名球员从池中移出
<?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);
我们要做的是,将玩家分成两组:竞争者1和竞争者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
如果一所学校的球员总数超过一半,会发生什么?好吧,让我们看看:
A A A A A
A B B C D
因此:
。。该方法也适用于>一半的时间:
C A
A A <= that one double again, unavoidable
A D
A B
A B
$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"
}
]
]
F A
F D
C D
C B
C B
A B
A E
A E
C A
A A <= that one double again, unavoidable
A D
A B
A B
//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);
[
[
{
"name": "juan",
"school": "ABC"
},
{
"name": "arnold",
"school": "DEF"
}
],
[
{
"name": "leo",
"school": "ABC"
},
{
"name": "ash",
"school": "ECD"
}
],
[
{
"name": "simon",
"school": "DEF"
},
{
"name": "luke",
"school": "ECD"
}
]
]