2

Im working on a code for generating a sports teams. I have an array with a list of players and want to store on another array all the possible team cobinations.

To make it simple, lets imagine a tennis match, where you have 4 players that will be split into two teams.

$players = array("Federer","Del Potro","Nadal","Murray");

The output array should look something like this:

$combinations[0][0] = "Federer","Del Potro";
$combinations[0][1] = "Nadal","Murray";

$combinations[1][0] = "Federer","Nadal";
$combinations[1][1] = "Del Potro","Murray";

$combinations[2][0] = "Del Potro","Nadal";
$combinations[2][1] = "Federer","Murray"; .. and so forth..

Any help?

Thanks in advance!

/// -- Edit

This is the code I have so far. All players also have a score and I store this score for later usage. Its not really important. I think I've got it working but im not sure this code gets ALL the possible combinations. What I do is I loop "player count" times and start building teams, after a team is built , I move the second player of the list to the bottom of the array and loop again.

 //-- Get the Max Players and the Array of Player Names
    $max_players = count($players)/2;
    $p = array_keys($players);

    for($i=0;$i<=(count($p));$i++){
        $tmp = array();
        $t=0;

        //-- Loop and start placing players into a team. When the max is reached, start placing on the other team.
        foreach($p as $player) {
            //-- Store player on Team
            $tmp[$t][] = $player;
            if(count($tmp[$t])==$max_players) {
                $t++;
            }
        }
        //-- Get the second player and move it to the bottom of the list.
        $second = $p[1];
        unset($p[1]);
        $p[] = $second;
        $p = array_values($p);

        //-- Loop thru teams and add the score of each player
        foreach($tmp as $key=>$eq) {
            $score = 0 ;
            foreach($eq as $jug) {
                //-- Add Score for each player
                $score +=  floatval($players[$jug]["score"]);
            }
            //-- Store the sum of scores of all players in team
            $tmp[$key]["score"] = $score;
        }
        //-- Store the Difference between team scores in this "team set"
        $tmp["diff"] = abs(round($tmp[0]["score"]-$tmp[1]["score"],2));
        $teams[] = $tmp;
    }
2
  • I have some code for a similar football team program, but mine I recall is for teams of 2 and 4 players, because is street football.
    – Nelson
    Mar 8, 2013 at 16:06
  • I've edited the code with my function so far
    – Bathan
    Mar 12, 2013 at 12:31

2 Answers 2

2

Just googled and found these results from stackoverflow.com

Get all combinations of a PHP array

algorithm that will take numbers or words and find all possible combinations

PHP: How to get all possible combinations of 1D array?

0
0

$player_combination = []; $match_combination = [];

  $players = array("Federer","Del Potro","Nadal","Murray");
  for($i = 0; $i< count($players);$i++){
      for($j=$i+1;$j<count($players);$j++){
        $player_combination[] = [$players[$i],$players[$j]]; 
       }
  }

  for($i = 0; $i< count($player_combination);$i++){
      for($j=$i+1;$j<count($player_combination);$j++){
        if(($player_combination[$i][0] !== $player_combination[$j][0]) &&  ($player_combination[$i][0] !== $player_combination[$j][1])&& ($player_combination[$i][1] !== $player_combination[$j][0]) && ($player_combination[$i][1] !== $player_combination[$j][1]))
        $match_combination[] = [$player_combination[$i],$player_combination[$j]]; 
       }
  }

my webiste is 9amobile.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.