스포츠 팀 생성을위한 코드 작업 중입니다. 나는 선수 명단이있는 배열을 가지고 있으며 가능한 모든 팀 구속을 다른 배열에 저장하려고합니다.두 개의 PHP 배열로 가능한 모든 조합을 얻으십시오.
간단하게하기 위해 테니스 경기를 상상해보십시오. 여기에는 4 명이 두 팀으로 나뉩니다.
$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..
어떤 도움 :
$players = array("Federer","Del Potro","Nadal","Murray");
출력 배열의 모양은?
미리 감사드립니다.
/// - 편집
이것은 지금까지 가지고있는 코드입니다. 모든 플레이어는 점수가 있으며 나중에 사용하기 위해이 점수를 저장합니다. 그다지 중요하지 않습니다. 나는 그것이 작동한다고 생각하지만,이 코드가 모든 가능한 조합을 얻는 지 확신하지 못한다. 내가하는 일은 "플레이어 수"를 반복하고 팀 구성을 시작하는 것입니다. 팀을 만든 후에는 목록의 두 번째 플레이어를 배열의 맨 아래로 이동시키고 다시 루프합니다.
//-- 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 명과 4 명이 팀을 이룹니다. – Nelson
지금까지 함수를 사용하여 코드를 편집했습니다 – Bathan