如何在PHP中随机填充单个锦标赛消除而不重复?

时间:2015-06-17 15:42:22

标签: php arrays random tournament

如果我有这个:

$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>';
}

?>

2 个答案:

答案 0 :(得分:6)

这应该适合你:

只需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>";

?>

答案 1 :(得分:2)

使用suffle函数随机化播放器的顺序,并按步骤2

读取数组
shuffle($players);

for ($x = 0; $x < count($players); $x += 2) {
  echo "Match " . (($x/2)+1) . ": " . $players[$x] . "x" . $players[$x+1] . "\n";
}