简体   繁体   中英

Get all possible combinations of a PHP Array split in two

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

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM