PHP数组-唯一匹配

| 大家好,我想知道您是否会对我的PHP逻辑有所帮助... 我想根据以下数据生成一个联赛表。 由user_id组成的数字数组 一个带有数值的变量,该数值指示两人之间要玩多少个独特的游戏。 我需要创建一个数组来反映每个独特的游戏。
player1 vs player2 
player1 vs player2
player1 vs player2
但是我不想像
player2 vs player 1 -
这将超过上面指定的最大游戏限制(即使玩家的顺序不同)。 我真的很努力地解决这个问题的逻辑,所以任何帮助都会很棒。 谢谢,     
已邀请:
        跟踪配对及其匹配,如下所示:
// $players - the players width ids
// $num_of_matches - max number of matches between 2 players

$match_counter = array();
$matches = array();
foreach ($players as $idA)
{
    foreach ($players as $idB)
    {
        if ($idA != $idB)
        {
            $match_id = $idA < $idB ? $idA.\"vs\".$idB : $idB.\"vs\".$idA;
            if (empty($match_counter[$match_id])) 
                $match_counter[$match_id] = 1
            elseif ($match_counter[$match_id] < $num_of_matches)
                $match_counter[$match_id] += 1;
            $match_id .= \"_\".$match_counter[$match_id];
            $matches[$match_id] = \"player\".$idA.\" vs player\".$idB;
        }
    }
}
$ matches将包含所有唯一匹配项。     
        我希望你这样讲:
$players = array(1, 2, 3);
$games = 3;

for($i=0; $i<$games; $i++){
    for($j=0; $j<(count($players)-1); $j++){
        for($k=1; $k<count($players); $k++){
            if($j != $k)
                echo \'player\' . $players[$j] . \' - player\' . $players[$k] . \'<br />\';
        }
    }
}
返回:
player1 - player2
player1 - player3
player2 - player3
player1 - player2
player1 - player3
player2 - player3
player1 - player2
player1 - player3
player2 - player3
    

要回复问题请先登录注册