3

Let me says this up front, I am not trying to get every combination.

I am trying to get every combination of 6 players per team without each player being on a new team with another player from their original team. example

$team1 = "John, Joey, Steve, Lee";
$team2 = "Tom, Alex, Billy";
$team3 = "Clint";
$team4 = "Alan, Jerry";
...
$team10 = "James, Corey, Paul, Benny";

John and Joey can not be in the future combinations; Tom and Billy cannot be in any future combinations together. But I am trying to get every combination of 1 player from each of the 10 teams. I have all ten teams in an associative array $team

I have been trying to use this function but I think I am on the wrong track.

function combinations($arrays, $i = 0) {
    if (!isset($arrays[$i])) {
        return array();
    }
    if ($i == count($arrays) - 1) {
        return $arrays[$i];
    }

    // get combinations from subsequent arrays
    $tmp = combinations($arrays, $i + 1);

    $result = array();

    // concat each array from tmp with each element from $arrays[$i]
    foreach ($arrays[$i] as $v) {
        foreach ($tmp as $t) {
            $result[] = is_array($t) ? 
                array_merge(array($v), $t) :
                array($v, $t);
        }
    }

    return $result;
}

print_r(
    combinations(
        array(
            $team[1],
            $team[2],
            $team[3],
            $team[4],
            $team[5],
            $team[6],
            $team[7],
            $team[8],
            $team[9],
            $team[10]
        )
    )
);
4
  • so $team1,$team2,...... have players in commmon Commented Aug 15, 2016 at 2:15
  • No I've eliminated any duplicates from the entire associative array. Each Team has different players. But in their new teams they cannot be on a team with their original teammates. Commented Aug 15, 2016 at 2:38
  • What are you doing with duplicate names? For example, if there are two people named "Chris" Commented Aug 15, 2016 at 2:42
  • They've been previously eliminated. Only one Chris is allowed to play, they second Chris has to go home. Commented Aug 15, 2016 at 2:47

1 Answer 1

1

Is this what you want?

        <?php
        $team1 = array("John", "Joey", "Steve", "Lee");
        $team2 = array("Tom", "Alex", "Billy");
        $team3 = array("Clint","a","c");
        $team4 = array("Alan", "Jerry");
        $team5 = array("John1", "Joey1", "Steve1", "Lee1");
        $team6 = array("Tom1", "Alex1", "Billy1");
        $team7 = array("Clint1","b","d");
        $team8 = array("Alan1", "Jerry1");
        $team9 = array("John2", "Joey2", "Steve2", "Lee2");
        $team10 = array("Tom2", "Alex2", "Billy2");


        $noTeams = 10;

        //$noNewTeams = 10;

        $membersPicked = array();
        $teamsDone = 0; // the whole team picked up
        $teamMemsDone = array();



        for($i=1; $i<11; $i++){
            $teamName = "team".$i;  
            $teamMemsDone["$teamName"] = 0;

        }






        $n=1;

        //for($n=1; $n<$noNewTeams+1; $n++) {
        //    $index = $n+1;

        do {

            unset($teamPicked);
            unset($newTeam);
            $newTeam = array();
            $teamPicked = array(); 

            //for($i=0; $i<6; $i++) {
            do {
                // pick up 6 team members for new team

                foreach($teamPicked as $key=>$value) {
                    //echo "$key => $value .... <br>";
                }


                do {
                    $teamNo = rand(1,$noTeams);
                }while(in_array($teamNo, $teamPicked) );

                array_push($teamPicked, $teamNo);

                $teamName = "team".$teamNo;
                $memsOfTeam = count($$teamName);

                $thisTeam = $$teamName;

                //echo "$teamNo, $teamName, $memsOfTeam <br/>";
                $member = "";
                $memberName = "";
                $noOfTimes = 0;
                do {
                    $member = array_rand($$teamName,1);
                    $memberName = $thisTeam[$member];
                    $noOfTimes ++;
                }while(in_array($memberName, $membersPicked) && $noOfTimes < $memsOfTeam);

                if($memberName != "") {
                    //echo $thisTeam[$member] ." <br/>";
                    array_push($membersPicked, $memberName);

                    $teamMemsDone["$teamName"] += 1; 

                    if($teamMemsDone["$teamName"] > $memsOfTeam) {
                        $teamsDone ++;

                        echo "$teamName - done. <br/>";
                    }

                    array_push($newTeam, $memberName);
                }


               // echo " new team members selected = " . count($newTeam) . "<br/>";


            } while(count($newTeam)<6);

            $newTeamName = "newTeam".$n;
            $$newTeamName = $newTeam;
            echo $newTeamName ."<br/>";

            $n++;


            foreach($$newTeamName as $key=>$val){
                echo "$key => $val <br/>";
            }
            echo "<br/>";

            echo $teamsDone ."<br/>";

        }while($teamsDone < $noTeams);

        //}


        ?>
Sign up to request clarification or add additional context in comments.

2 Comments

I'll look at this closer when I get home,but it looks like this will yield only 10 results. I'm looking for all teams I can make without any current teammates being together on a new team.
Sorry it took me so long to respond, but I've been researching this a bit more and It turns out I am wanting a Cartesian Product. But I Love this Random function you made here. That's good stuff! I don't understand what each line is doing could you explain section of function is doing please?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.