Php 排序循环匹配
我有一个循环算法,用于创建团队之间的匹配数组。每场比赛都在主队所在地进行 一旦比赛开始了,我想把两队主场位置相同的比赛转移到第一轮 团队的示例阵列:Php 排序循环匹配,php,Php,我有一个循环算法,用于创建团队之间的匹配数组。每场比赛都在主队所在地进行 一旦比赛开始了,我想把两队主场位置相同的比赛转移到第一轮 团队的示例阵列: $teams = array( 'Team A' => 'Green Field', 'Team B' => 'Blue Field', 'Team C' => 'Green Field', 'Team D' => 'Red Field', 'Team E' => 'Blue Field', );
$teams = array(
'Team A' => 'Green Field',
'Team B' => 'Blue Field',
'Team C' => 'Green Field',
'Team D' => 'Red Field',
'Team E' => 'Blue Field',
);
和匹配项:
-第一轮
A队vs D队(绿地)
B队对E队(蓝场)
-第二轮
A队对B队(绿地)
C队对D队(绿地)
-第二轮
A队vs C队(绿地)
E队对D队(蓝场)
-第二轮
E队对C队(蓝场)
D队对B队(红场)
-第二轮
A队vs E队(绿地)
B队对C队(蓝场)
在这种情况下,我们将把A队和C队移动到第1轮,因为他们都有相同的位置(绿色区域)。然而,我们需要将A队和d队转移到其他地方,因为每一队每轮只能打一场比赛。在这个例子中,它可能不需要做很多调整,但是当有10多个团队参与时,它会变得非常棘手。特别是当有两支以上的球队在同一主场时
下面是我目前实现的实际循环算法
// Participant IDs are unique integer identifiers for teams.
// Example array: $participants = array(1 => 'Joe', 2 => 'Marc') etc.
$ids = array_keys($participants);
$count = count($ids);
// We must add a dummy participant for odd numbers
if ($count % 2) {
$ids[] = 0;
$count++;
}
// There are n/2 matches per round
$match_count = $count / 2;
// Create matches for each round.
// Example array: $rounds = array(1 => '01/01/2015', 2 => '02/01/2015') etc.
foreach ($rounds as $round => $date) {
$temp_ids = $ids;
for ($i=0;$i<$match_count;$i++) {
// Pick 2 competitors
$a = array_shift($temp_ids);
$b = array_pop($temp_ids);
// Only create matches without dummy participants
if ($a && $b) {
// Initialize the match
$match = array(
'date' => $date,
'participants' => array($a, $b),
'location' => get_location($a),
);
$matches[] = $match;
}
}
// Move the last id from participants after the first
$id = array_pop($ids);
array_splice($ids, 1, 0, $id);
}
//参与者ID是团队的唯一整数标识符。
//示例数组:$participants=array(1=>'Joe',2=>'Marc')等。
$ids=数组_键($participants);
$count=计数($id);
//我们必须为奇数添加一个虚拟参与者
如果($count%2){
$ids[]=0;
$count++;
}
//每轮有n/2场比赛
$match_count=$count/2;
//为每一轮创建匹配项。
//示例数组:$rounds=array(1=>'01/01/2015',2=>'02/01/2015')等。
foreach($round为$round=>$date){
$temp_id=$id;
对于($i=0;$i$日期,
“参与者”=>数组($a,$b),
“位置”=>获取位置($a),
);
$matches[]=$match;
}
}
//将参与者的最后一个id移到第一个id之后
$id=array\u pop($id);
阵列拼接($ids,1,0,$id);
}
这就是我要使用的:
定义9个团队:
$teams = array(
'Team A' => 'Green Field',
'Team B' => 'Blue Field',
'Team C' => 'Green Field',
'Team D' => 'Red Field',
'Team E' => 'Blue Field',
'Team F' => 'Green Field',
'Team G' => 'Yellow Field',
'Team H' => 'Blue Field',
'Team I' => 'Red Field',
);
创建3个数组来存储所有匹配项、所有主位置以及最后所有轮次
$matches = array();
$locations = array();
$rounds = array();
这就是你所说的:获得所有匹配项。(我也会存储团队的总部位置,以备日后使用)
然后我们对$matches
数组进行排序,以便具有相同主位置的所有团队都位于顶部
usort($matches, function($a, $b) {
$keysA = array_keys($a);
$keysB = array_keys($b);
return ($b[$keysB[0]] === $b[$keysB[1]]) - ($a[$keysA[0]] === $a[$keysA[1]]);
});
然后我们可以循环所有的比赛,只要还有一些比赛要打。我们只是使用可能的位置作为一个指标,如果他们仍然是免费的
while(!empty($matches)) {
array_push($rounds,$locations);
foreach($rounds[max(array_keys($rounds))] as $location => &$match) {
foreach($matches as $key => $val) {
$keys = array_keys($val);
if($val[$keys[0]] === $location || $val[$keys[1]] === $location) {
$match = $matches[$key];
unset($matches[$key]);
continue 2;
}
}
}
}
注意:如果您无法理解我在这里做了什么,请提问,我将添加一些描述:)
这是:
print_r($rounds);
会给你这个怪物:
Array
(
[0] => Array
(
[Green Field] => Array
(
[Team C] => Green Field
[Team F] => Green Field
)
[Blue Field] => Array
(
[Team B] => Blue Field
[Team H] => Blue Field
)
[Red Field] => Array
(
[Team D] => Red Field
[Team I] => Red Field
)
[Yellow Field] => Array
(
[Team E] => Blue Field
[Team G] => Yellow Field
)
)
[1] => Array
(
[Green Field] => Array
(
[Team A] => Green Field
[Team F] => Green Field
)
[Blue Field] => Array
(
[Team E] => Blue Field
[Team H] => Blue Field
)
[Red Field] => Array
(
[Team D] => Red Field
[Team H] => Blue Field
)
[Yellow Field] => Array
(
[Team D] => Red Field
[Team G] => Yellow Field
)
)
[2] => Array
(
[Green Field] => Array
(
[Team A] => Green Field
[Team C] => Green Field
)
[Blue Field] => Array
(
[Team B] => Blue Field
[Team E] => Blue Field
)
[Red Field] => Array
(
[Team D] => Red Field
[Team F] => Green Field
)
[Yellow Field] => Array
(
[Team F] => Green Field
[Team G] => Yellow Field
)
)
[3] => Array
(
[Green Field] => Array
(
[Team E] => Blue Field
[Team F] => Green Field
)
[Blue Field] => Array
(
[Team H] => Blue Field
[Team I] => Red Field
)
[Red Field] => Array
(
[Team A] => Green Field
[Team D] => Red Field
)
[Yellow Field] => Array
(
[Team G] => Yellow Field
[Team I] => Red Field
)
)
[4] => Array
(
[Green Field] => Array
(
[Team F] => Green Field
[Team I] => Red Field
)
[Blue Field] => Array
(
[Team G] => Yellow Field
[Team H] => Blue Field
)
[Red Field] => Array
(
[Team D] => Red Field
[Team E] => Blue Field
)
[Yellow Field] => Array
(
[Team B] => Blue Field
[Team G] => Yellow Field
)
)
[5] => Array
(
[Green Field] => Array
(
[Team F] => Green Field
[Team H] => Blue Field
)
[Blue Field] => Array
(
[Team E] => Blue Field
[Team I] => Red Field
)
[Red Field] => Array
(
[Team C] => Green Field
[Team I] => Red Field
)
[Yellow Field] => Array
(
[Team C] => Green Field
[Team G] => Yellow Field
)
)
[6] => Array
(
[Green Field] => Array
(
[Team B] => Blue Field
[Team F] => Green Field
)
[Blue Field] => Array
(
[Team B] => Blue Field
[Team D] => Red Field
)
[Red Field] => Array
(
[Team A] => Green Field
[Team I] => Red Field
)
[Yellow Field] => Array
(
[Team A] => Green Field
[Team G] => Yellow Field
)
)
[7] => Array
(
[Green Field] => Array
(
[Team B] => Blue Field
[Team C] => Green Field
)
[Blue Field] => Array
(
[Team A] => Green Field
[Team H] => Blue Field
)
[Red Field] => Array
(
[Team B] => Blue Field
[Team I] => Red Field
)
[Yellow Field] => Array
(
)
)
[8] => Array
(
[Green Field] => Array
(
[Team A] => Green Field
[Team E] => Blue Field
)
[Blue Field] => Array
(
[Team C] => Green Field
[Team H] => Blue Field
)
[Red Field] => Array
(
[Team C] => Green Field
[Team D] => Red Field
)
[Yellow Field] => Array
(
)
)
[9] => Array
(
[Green Field] => Array
(
[Team A] => Green Field
[Team B] => Blue Field
)
[Blue Field] => Array
(
[Team C] => Green Field
[Team E] => Blue Field
)
[Red Field] => Array
(
)
[Yellow Field] => Array
(
)
)
)
由于您只希望每轮有2场比赛,因此可以使用以下选项(而不是上次的,而:
// set matches per round
$mPerR = 2;
while(!empty($matches)) {
$keys = array_keys($matches[0]);
$taken = array($matches[0][$keys[0]]);
array_push($rounds,array($matches[0][$keys[0]] => $matches[0]));
array_shift($matches);
for($i=1;$i<$mPerR;$i++) {
foreach($matches as $key => $val) {
$keys = array_keys($val);
switch(true) {
case(!in_array($val[$keys[0]],$taken)):
$location = $val[$keys[0]];
break;
case(!in_array($val[$keys[1]],$taken)):
$location = $val[$keys[1]];
break;
default:
continue 2;
}
array_push($taken,$location);
$rounds[max(array_keys($rounds))][$location] = $matches[$key];
unset($matches[$key]);
$matches = array_values($matches);
continue 2;
}
}
}
你能给你演示一下算法吗?谢谢你花时间写下这个详细的答案。但是,我对它的实现有点困惑。我已经用我用来创建匹配的实际循环算法更新了我的问题。这是在参与者之间获得均匀匹配的最佳方法,它还允许我如果每个参与者需要多次玩游戏,则需要多次传球。无论如何,您是否能够更新您的答案以调整代码?您是否可以添加您使用的全部代码?因为您提供的代码中缺少一些数组。数组正是您所期望的。我添加了一些注释,以给出示例它们将包含的数据。您可以使用get_location()回调返回团队位置。是的……但我从哪里知道哪些位置可用?参与者没有“主位置”
// set matches per round
$mPerR = 2;
while(!empty($matches)) {
$keys = array_keys($matches[0]);
$taken = array($matches[0][$keys[0]]);
array_push($rounds,array($matches[0][$keys[0]] => $matches[0]));
array_shift($matches);
for($i=1;$i<$mPerR;$i++) {
foreach($matches as $key => $val) {
$keys = array_keys($val);
switch(true) {
case(!in_array($val[$keys[0]],$taken)):
$location = $val[$keys[0]];
break;
case(!in_array($val[$keys[1]],$taken)):
$location = $val[$keys[1]];
break;
default:
continue 2;
}
array_push($taken,$location);
$rounds[max(array_keys($rounds))][$location] = $matches[$key];
unset($matches[$key]);
$matches = array_values($matches);
continue 2;
}
}
}
Array
(
[0] => Array
(
[Blue Field] => Array
(
[Team B] => Blue Field
[Team E] => Blue Field
)
[Green Field] => Array
(
[Team A] => Green Field
[Team C] => Green Field
)
)
[1] => Array
(
[Green Field] => Array
(
[Team C] => Green Field
[Team D] => Red Field
)
[Red Field] => Array
(
[Team D] => Red Field
[Team E] => Blue Field
)
)
[2] => Array
(
[Green Field] => Array
(
[Team C] => Green Field
[Team E] => Blue Field
)
[Blue Field] => Array
(
[Team B] => Blue Field
[Team C] => Green Field
)
)
[3] => Array
(
[Green Field] => Array
(
[Team A] => Green Field
[Team D] => Red Field
)
[Blue Field] => Array
(
[Team A] => Green Field
[Team E] => Blue Field
)
)
[4] => Array
(
[Green Field] => Array
(
[Team A] => Green Field
[Team B] => Blue Field
)
[Blue Field] => Array
(
[Team B] => Blue Field
[Team D] => Red Field
)
)
)