如何在 PHP 中随机填充单个锦标赛淘汰而不重复?
How to populate a single tournament elimination randomly in PHP without repeat?
如果我有这个:
$players = array("A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q");
我如何像这样填充单个锦标赛淘汰赛:
Matche 1: AxL
Matche 2: CxJ
Matche 3: HxQ
.
.
.
Matche 8: ExP
16 名玩家 = 8 场比赛
我也试过这个和其他代码:
<?php
$players = array("A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q");
shuffle ($players);
foreach($players as $key=>$value)
{
echo $value.','.$value.'<br>';
}
?>
使用shuffle函数将玩家顺序随机化,以2为步长读取数组
shuffle($players);
for ($x = 0; $x < count($players); $x += 2) {
echo "Match " . (($x/2)+1) . ": " . $players[$x] . "x" . $players[$x+1] . "\n";
}
这应该适合你:
只需 shuffle()
您的数组,然后 array_chunk()
将其分成 2 组,例如
<?php
$players = ["A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q"];
shuffle($players);
$players = array_chunk($players, 2);
foreach($players as $match => $player)
echo "Match " . ($match+1) . ": " . $player[0] . "x" . $player[1] . "<br>";
?>
如果我有这个:
$players = array("A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q");
我如何像这样填充单个锦标赛淘汰赛:
Matche 1: AxL
Matche 2: CxJ
Matche 3: HxQ
.
.
.
Matche 8: ExP
16 名玩家 = 8 场比赛
我也试过这个和其他代码:
<?php
$players = array("A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q");
shuffle ($players);
foreach($players as $key=>$value)
{
echo $value.','.$value.'<br>';
}
?>
使用shuffle函数将玩家顺序随机化,以2为步长读取数组
shuffle($players);
for ($x = 0; $x < count($players); $x += 2) {
echo "Match " . (($x/2)+1) . ": " . $players[$x] . "x" . $players[$x+1] . "\n";
}
这应该适合你:
只需 shuffle()
您的数组,然后 array_chunk()
将其分成 2 组,例如
<?php
$players = ["A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q"];
shuffle($players);
$players = array_chunk($players, 2);
foreach($players as $match => $player)
echo "Match " . ($match+1) . ": " . $player[0] . "x" . $player[1] . "<br>";
?>