2016-08-15 79 views
3

讓我先說這一點,我並不是想要獲得所有組合。製作多個陣列的每個組合,而不必在每個陣列中重複項目

我試圖讓每個隊伍有6名球員的每一個組合,而沒有每個球員與另一名球員在一個新的球隊中。 例如

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

John和喬伊不能在未來的組合;湯姆和比利不能在將來組合在一起。但我試圖從10支球隊的每一支中獲得每個球員的組合。 我有一個聯合陣列的所有十個球隊$團隊

我一直在嘗試使用這個功能,但我認爲我在錯誤的軌道上。

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] 
     ) 
    ) 
); 
+0

所以$ TEAM1,$ TEAM2,......有玩家在commmon – coder

+0

不,我已經消除,從整個關聯數組任何重複。每個球隊都有不同的球員。但是在他們的新球隊中,他們不能與原隊友一起組成球隊。 – Kaff1n8t3d

+0

你在做重複的名字?例如,如果有兩個人名叫「Chris」 – Chris

回答

1

這是你想要的嗎?

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

     //} 


     ?> 
+0

當我回家的時候,我會仔細看看這個,但是看起來這隻會產生10個結果。我正在尋找所有我可以在沒有任何現任隊友加入新隊伍的隊伍中完成的任務。 – Kaff1n8t3d

+0

編輯答案...請檢查...不確定這是否正常工作... –

+0

對不起,我花了這麼長的時間來回應,但我一直在研究這個更多,事實證明我想要笛卡爾積。但我喜歡你在這裏做的隨機功能。這是好東西!我不明白每一行在做什麼,你能解釋一下功能部分在做什麼嗎? – Kaff1n8t3d