PHP 数组 - 唯一匹配

发布于 2024-11-10 15:28:01 字数 412 浏览 7 评论 0原文

嘿伙计们,我想知道你们是否能帮我解决一下我的 PHP 逻辑...

我想根据以下数据生成一个联赛表..

  • 一个由 user_ids 组成的数字数组
  • ,一个带有指示有多少个数值的变量两人之间要玩的独特游戏..

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

player1 vs player2 
player1 vs player2
player1 vs player2

但是我不希望出现诸如

player2 vsplayer1 -之类的欺骗行为,这将超过最大游戏数即使玩家的顺序不同,上面也将 limit 指定为变量。

我真的很难理解这个问题的逻辑,所以任何帮助都会很棒..

谢谢,

Hey guys, i was wondering if you would help me a little with my PHP logic...

I would like to generate a league table based on the following data..

  • A numeric array consisting of user_ids
  • a variable with a numeric value indicating how many unique games to be played between the pair..

I need to created an array to reflect each unique game..

player1 vs player2 
player1 vs player2
player1 vs player2

However i do not want dupes such as

player2 vs player 1 - This would be over the maximum game limit specified above as a variable even though the players are in a different order.

I am really struggling to wrap my head around the logic of this problem, so any help would be great..

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2024-11-17 15:28:01

跟踪配对及其匹配,如下所示:

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

Track the pairs, and their matches like this:

// $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 will contain all the unique matches.

可是我不能没有你 2024-11-17 15:28:01

我希望你这样修改:

$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

i hope you ment it that way:

$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 />';
        }
    }
}

returns:

player1 - player2
player1 - player3
player2 - player3
player1 - player2
player1 - player3
player2 - player3
player1 - player2
player1 - player3
player2 - player3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文