gpt4 book ai didi

php - 制作多个数组的每个组合,而不重复每个数组中的项目

转载 作者:搜寻专家 更新时间:2023-10-31 21:24:40 24 4
gpt4 key购买 nike

让我先说明一下,我并不是要获得所有组合。

我正在尝试获得每支球队 6 名球员的每一种组合,而不是每个球员都与他们原来球队的另一名球员在一个新球队中。例子

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

约翰和乔伊以后不能再组合了;汤姆和比利不能在未来的任何组合中在一起。但我正在尝试从 10 支球队中获得 1 名球员的每一种组合。我有一个关联数组 $team

中的所有十个团队

我一直在尝试使用这个功能,但我认为我走错了路。

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

最佳答案

这是你想要的吗?

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

//}


?>

关于php - 制作多个数组的每个组合,而不重复每个数组中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38948345/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com