php - 需要逻辑帮助,制作三维数组(PHP)

标签 php round-robin

我需要帮助,例如,我在那里得到了包含 4 的团队名称数组。

array('logiX.L4d22','Lust','Marat and Friends','Pandas of Belgium');

我想制作一个三维数组,其中第一个是一轮,第二个是一场比赛,第三个是互相比赛的球队,总是有 2 支球队。

逻辑必须使所有球队必须与所有其他球队一起比赛,并且在一轮中,任何球队只能参加一场比赛,所以如果我们有 5 支球队,那么在某一轮中,一支球队必须等待下一轮。

它必须产生这样的结果:

array
  0 => 
    array
      0 => 
        array
          0 => string 'logiX.L4D2' (length=10)
          1 => string 'Lust' (length=4)
      1 => 
        array
          0 => string 'Marat and Friends' (length=17)
          1 => string 'Pandas of Belgium' (length=17)
  1 => 
    array
      0 => 
        array
          0 => string 'logiX.L4D2' (length=10)
          1 => string 'Marat and Friends' (length=17)
      1 => 
        array
          0 => string 'Lust' (length=4)
          1 => string 'Pandas of Belgium' (length=17)
  2 => 
    array
      0 => 
        array
          0 => string 'logiX.L4D2' (length=10)
          1 => string 'Pandas of Belgium' (length=17)
      1 => 
        array
          0 => string 'Lust' (length=4)
          1 => string 'Marat and Friends' (length=17)

必须与 2,3,5 ...10 ...12 个团队合作。

我希望你能帮助我,我已经花了 1 天半的时间了。

最佳答案

循环算法 PHP 进行一些谷歌搜索,得出以下结果:

http://speedtech.it/blog/2009/03/15/round-robin-algorithm-php/

http://www.phpbuilder.com/board/showthread.php?t=10300945

希望您能找到您想要的东西。

编辑

按照round-robin algorithm described on Wikipedia添加我对此的尝试.

如果队伍数量为奇数,则会在数组中添加一个队伍(空值),这样您就可以检索每轮的“等待队伍”。

<?php

$teams = range('a', 'g');

function make_rounds($teams)
{
  $nb_teams = count($teams);

  if ($nb_teams % 2 != 0)
  {
    $teams[] = null;
    $nb_teams++;
  }

  $nb_rounds = $nb_teams - 1;
  $nb_matches = $nb_teams / 2;

  $rounds = array();

  for($round_index = 0; $round_index < $nb_rounds; $round_index++)
  {
    $matches = array();

    for($match_index = 0; $match_index < $nb_matches; $match_index++)
    {
      if ($match_index == 0)
        $first_team = $teams[0];
      else
        $first_team = $teams[(($nb_teams-2) + $match_index - $round_index) % ($nb_teams-1) + 1];

      $second_team = $teams[(($nb_teams*2) - $match_index - $round_index - 3) % ($nb_teams-1) + 1];

      $matches[] = array($first_team, $second_team);
    }

    $rounds[] = $matches;
  }

  return $rounds;
}

print_r(make_rounds($teams));

关于php - 需要逻辑帮助,制作三维数组(PHP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5485728/

相关文章:

linux - Linux 中的实时调度

c - Round Robin C 调度模拟器

java - JVM DNS 缓存和 DNS 循环

mysql - MySQL 有没有办法对变量 Universe 中的特定字段执行 “round robin” ORDER BY ?

javascript - 在 bonfire codeigniter 中添加按钮以将表格打印到打印机

javascript - Charts js php 实现按多货币时间戳分组

php - 使用 PHP 生成 PDF

database - 循环分配实现(数据库)

php - 更改默认网站语言

php - XAMPP (Windows) PHP GMP 函数