Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP循环生成创建“重复”_Php_Algorithm_Round Robin - Fatal编程技术网

PHP循环生成创建“重复”

PHP循环生成创建“重复”,php,algorithm,round-robin,Php,Algorithm,Round Robin,我在PHP中实现了一个循环生成器,用于在联盟中的用户之间创建装置。这将匹配联盟中的所有用户,并在每个游戏周让他们相互竞争 代码如下所示 /** * Rotates an array for the round robin algorithm */ function round_robin_array($array) { // we always keep index 0 $top = array_shift($array

我在PHP中实现了一个循环生成器,用于在联盟中的用户之间创建装置。这将匹配联盟中的所有用户,并在每个游戏周让他们相互竞争

代码如下所示

    /**
     * Rotates an array for the round robin algorithm
     */
    function round_robin_array($array)
    {
        // we always keep index 0
        $top = array_shift($array);
        $last = array_pop($array);
        $rotate = [$last];
        foreach ($array as $_value) {
            $rotate[] = $_value;
        }
        array_unshift($rotate, $top);
        return $rotate;
    }

    /**
     * Runs a round robin to make a schedule.
     */
    function round_robin($users, $weeks)
    {
        $schedule = [];
        $count = count($users);
        foreach ($users as $_u) {
            $schedule[$_u] = array_fill(0, $weeks, []);
        }
        for ($i=0;$i<$weeks;$i++) {
            for ($a=0;$a<($count / 2) + 1;$a++) {
                $vs = $users[$a];
                $opp = $users[($count - $a) - 1];
                $at = rand(0,4);
                $pg = [$opp, $at];
                $og = [$vs, $at];
                $schedule[$vs][$i] = $pg;
                $schedule[$opp][$i] = $og;
            }
            $users = $this->round_robin_array($users);
        }
        return $schedule;
    }

    public function generateFixtures($league, $users, $weeks)
    {
        $array = [];

        foreach($users as $user) {
            array_push($array, $user['id']);
        }

        if(count($array) % 2 != 0) {
            array_push($array, '0');
        }

        $fixtures = $this->round_robin($array, $weeks);

        $gameweek = 1;

        foreach($fixtures as $key => $val) {
            if($key != 0) {
                foreach($val as $opponent) {
                    LeagueFixture::firstOrCreate([
                        'gameweek' => $gameweek,
                        'league_id' => $league,
                        'user_id' => $key,
                        'opponent_id' => $opponent[0],
                    ]);
                    $gameweek = $gameweek+1;
                }
            }
            $gameweek = 1;
        }

        return $fixtures;

    }
GenerateTextures函数传递给联盟的用户、联盟本身和周数

问题是,这会为每个装置创建“副本”,因为实际上每个用户都有一个视图——例如

游戏周1

用户1与用户2 用户3对用户4 用户2与用户1 用户4对用户3 正如你所看到的,最后两个装置是不同的;但还是一样


我的问题是,这是否是循环的问题,或者我是否可以/应该在控制器/视图中筛选这些重复项。这不是最优雅的解决方案,但在数据库的数据输入点,我可以检查是否已经存在与即将创建的内容相反的内容

            foreach($fixtures as $key => $val) {
                if($key != 0) {
                    //loop through the values to create an entry for each
                    foreach($val as $opponent) {
                        //check if the opposite of each entry already exists
                        if(!LeagueFixture::where('gameweek', $gameweek)
                            ->where('league_id', $league)
                            ->where('user_id', $opponent[0])
                            ->where('opponent_id', $key)
                            ->exists()) {
                            //if not, we can create the entry as usual
                            LeagueFixture::firstOrCreate([
                                'gameweek' => $gameweek,
                                'league_id' => $league,
                                'user_id' => $key,
                                'opponent_id' => $opponent[0],
                            ]);
                        }
                        $gameweek = $gameweek+1;
                    }
                }
                $gameweek = 1;
            }

这是我现在最简单的解决办法。。尽管数据是在上述循环中生成的,您可能会很容易地认为这是一种不好的做法,但重复的数据永远不会进入我的数据库

创建数组的副本,在其中查找对。找到一对后,从副本中删除这些,然后找到下一对。继续,直到筋疲力尽。谢谢@Mike'Pomax'Kamermans。你建议我什么时候做?在循环创建期间或为视图输出呈现数据时?避免这种情况的最佳方法是使用有序对,即对每个对p1、p2 p1