3

如果我有这个:

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

?>
4

2 回答 2

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

?>
于 2015-06-17T16:03:05.817 回答
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";
}
于 2015-06-17T16:01:24.713 回答