3

If i have this:

$players = array("A","B","C","D","E","F","G","H","I","J","L","M","N","O","P","Q");

How i populate a single tournament elimination like this for example:

Matche 1: AxL
Matche 2: CxJ
Matche 3: HxQ
.
.
.
Matche 8: ExP

16 players = 8 Matches

I try this and other codes too:

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

?>
FBN
  • 185
  • 1
  • 1
  • 11

2 Answers2

6

This should work for you:

Just shuffle() your array and then array_chunk() it into groups of 2, e.g.

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

?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Rizier123
  • 58,877
  • 16
  • 101
  • 156
2

Use the suffle function to randomize the order of the players and read the array by steps of 2

shuffle($players);

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