-2

I started learning and I am making a project for a home fifa18 cup. I have one problem, I don't know how I can pair all players I mean: Player1, Player2, Player3, Player4

=> P1 vs P2, P1 vs P3, P1 vs P4 
=> P2 vs P3, P2 vs P4 
=> P3 vs P4

I use Newton formula n!/k!(n-k)! to count how many match without revenge will be and have all players in $tab.

So now, my question, how can I pair this?

Like that and it will be not repetition & P1 vs P1 example.

<?php
    require("server/baza.php");
    $przelacznik = 1;//$_POST['losuj'];
    $zapytanie = "select nick from mecze";
    if($przelacznik == 1){
        $wynik = mysqli_query($polaczenie,$zapytanie);
        $tab = array();
        $n=0;
        while($wiersz = mysqli_fetch_assoc($wynik)){ 
            array_push($tab,$wiersz['nick']);
            //$tab[] = $wiersz;
            $n++;  
        }
        $Nsilnia = 1;
        $NKsilnia =1;
        for ($i=1; $i<=$n; $i++) {
            $Nsilnia *= $i;
        }
        for($j=1;$j<=($n-2);$j++){
            $NKsilnia *= $j;
        }
        $ilosc_rozgrywek = ($Nsilnia)/(2*$NKsilnia);
}
?>
shapir
  • 23
  • 2
  • 2
    Not enough information provided. Where do you store your players? What have you tried so far? – Twinfriends May 29 '18 at 09:59
  • 1
    Possible duplicate of https://stackoverflow.com/questions/10834393/php-how-to-get-all-possible-combinations-of-1d-array – Mickaël Leger May 29 '18 at 10:02
  • @Shapir they are down voting your question because they want to see your effort first, nobody codes entirely for you, they will correct you, so show your effort first. – BILAL MALIK May 29 '18 at 10:03
  • And before posting, why you don't try to look for some solution? I just googled "php get all combinaison in array" and find lot of thing that could help you :) – Mickaël Leger May 29 '18 at 10:06
  • 1
    @BILALMALIK im so sorry, i dont knew how this website work,I thought i should just ask, i serch but my english isn't good and i dont know how should i phrase my question. I edit my post and give what i wrote. – shapir May 29 '18 at 11:58
  • @MickaelLeger yeah, im so sorry, you have full right, but i didn't knew what phrase should i use. i tryed "PHP football match" / "PHP shuffle teams" but i cant find nothing, im new here and i still learning about php :( thank you for help! – shapir May 29 '18 at 12:04
  • @shapir np, just you have chance to be down voating if you ask a question with possible duplicate we can find fast + no code provided ! Think about it next time ;) – Mickaël Leger May 29 '18 at 12:11

4 Answers4

6

If you store your player in an array like:

$player = ['P1', 'P2', 'P3', 'P4'];

you can use two loops to pair all possible matches without revenge.

for ($i = 0; $i < count ($player); $i++) {

    //set the start of the second loop to $i + 1 
    for ($j = $i + 1; $j < count ($player); $j++) {

      echo $player[$i].' vs '.$player[$j];

    }

}

that will output:

// P1 vs P2
// P1 vs P3
// P1 vs P4
// P2 vs P3
// P2 vs P4
// P3 vs P4
wayneOS
  • 1,427
  • 1
  • 14
  • 20
2
$players = [ 'p1', 'p2','p3','p4', 'p5'];
$result =[];
for($i = 0; $i < count($players); $i++ ) {
    for($j=$i+1; $j<count($players); $j++) {
        $result[] = $players[$i] . ' vs ' . $players[$j];
    }
}
print_r($result);
Shivrudra
  • 674
  • 4
  • 5
2

If P1 vs P2 and P2 vs P1 are not same. Then use this one.

$player_list = ['p1', 'p2', 'p3', 'p4'];
$player_pair = array();
foreach($player_list as $key => $player){
    for($i = 0; $i < count($player_list); $i++ ){
        if($i != $key){
            $player_pair[] =  $player_list[$i] . ' VS ' . $player;
        }
    }
}
print_r($player_pair);

ELSE USE THIS ONE:

$player_list = ['p1', 'p2', 'p3', 'p4'];
$player_pair = array();
foreach($player_list as $key => $player){
    for($i = 0; $i < count($player_list); $i++ ){
        $str = $player_list[$i] . ' VS ' . $player;
        $str2 = $player . ' VS ' . $player_list[$i];
        if($i != $key && !in_array($str, $player_pair) && !in_array($str2, $player_pair)){
            $player_pair[] =  $player_list[$i] . ' VS ' . $player;
        }
    }
}
print_r($player_pair);
0
<?php
$no_players = 4;
$pairings   = [];

for($i = 1; $i <= $no_players; $i++)
    for($j = 1; $j <= $no_players; $j++)
        if($i !== $j && !in_array([$j, $i], $pairings))
            $pairings[] = [$i, $j];
var_export($pairings);

Output:

array (
    0 => 
    array (
      0 => 1,
      1 => 2,
    ),
    1 => 
    array (
      0 => 1,
      1 => 3,
    ),
    2 => 
    array (
      0 => 1,
      1 => 4,
    ),
    3 => 
    array (
      0 => 2,
      1 => 3,
    ),
    4 => 
    array (
      0 => 2,
      1 => 4,
    ),
    5 => 
    array (
      0 => 3,
      1 => 4,
    ),
  )
Progrock
  • 7,373
  • 1
  • 19
  • 25