2011-05-30 114 views
0

嗨,我在想,如果你能帮我一点与我的PHP逻辑...PHP阵列 - 独特的匹配

我想基于下列数据的排名表..

  • 的数字数组组成user_ids的
  • 用一个数字值表示多少独特的游戏的一对之间进行播放的可变..

我需要创建一个数组,以反映每一个独特的游戏..

player1 vs player2 
player1 vs player2 
player1 vs player2 

但是我不想愚弄如

player2 vs player 1 -这将是在上面,即使球员们以不同的顺序变量指定的最大游戏限额。

我真的很努力总结我解决这个问题的逻辑头,所以任何帮助将是巨大的..

感谢,

+0

那么一个“自制”的规则是什么?具有较低用户标识的玩家总是先来?像玩家1000 vs玩家1001? – sled 2011-05-30 15:30:42

回答

2

跟踪对,他们的比赛是这样的:

// $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将包含所有唯一匹配项。

1

我希望你彪这样的说法:

$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