dsf487787 2017-01-21 11:23 采纳率: 100%
浏览 24

拆分数组,随机随机播放

Does anyone know how I can split an array into groups of two, without any group(team in this case) meeting eachother again till running out of possibilities?

array [ 0, 1, 2, 3, 4, 5 ]

  • 写回答

2条回答 默认 最新

  • dqg2269 2017-01-21 13:21
    关注

    This is definitely not the optimal way to do this, please optimize it yourself (this will work for only even number of teams):

     <?php
        $allTeams = [0,1,2,3,4,5];
        $everyGames = [];//Append team matches to this
        foreach($allTeams as $team1){
            foreach($allTeams as $team2){
                if (!in_array([$team2, $team1], $everyGames)
                    && $team2 !== $team1) {
                    $everyGames[] = [$team1, $team2];
                }               
    
            }
        }
        $copyOfEveryGames = $everyGames;
        $needToLoopAgain = true;
        while($needToLoopAgain ){
            $allRounds = [];
            $needToLoopAgain = false;
            $everyGames  = $copyOfEveryGames;
            shuffle($everyGames);//Shuffle it for the random array
            while(count($everyGames) > 0){
                $allMatches = [];
                $tempEveryGames = $everyGames;
                for($i = 0; $i < count($allTeams) / 2; $i++){
                    foreach($tempEveryGames as $key => $game){
                        if(!(in_array_r($game[0], $allMatches) || in_array_r($game[1], $allMatches))){
                            $allMatches[] = $game;
                            unset($tempEveryGames[$key]);
                            break;
                        }
                    }
                }   
                if(count($allMatches) === count($allTeams) / 2){
                    $everyGames = $tempEveryGames ;
    
                    $allRounds[] = $allMatches;
                }else{
                    $needToLoopAgain = true;
                    break;
                }
            }
            if(count($allRounds) !== count($allTeams) -1){
                $needToLoopAgain = true;
            }
        }
    
        //Display Example
        foreach($allRounds as $round){
            print_r($round);
            echo "<br>";
        }
        /*
        Example Output:
        Array ( [0] => Array ( [0] => 0 [1] => 3 ) 
                [1] => Array ( [0] => 1 [1] => 4 ) 
                [2] => Array ( [0] => 2 [1] => 5 ) ) 
        Array ( [0] => Array ( [0] => 0 [1] => 2 ) 
                [1] => Array ( [0] => 4 [1] => 5 ) 
                [2] => Array ( [0] => 1 [1] => 3 ) ) 
        Array ( [0] => Array ( [0] => 1 [1] => 2 ) 
                [1] => Array ( [0] => 3 [1] => 4 ) 
                [2] => Array ( [0] => 0 [1] => 5 ) ) 
        Array ( [0] => Array ( [0] => 1 [1] => 5 ) 
                [1] => Array ( [0] => 0 [1] => 4 ) 
                [2] => Array ( [0] => 2 [1] => 3 ) ) 
        Array ( [0] => Array ( [0] => 0 [1] => 1 ) 
                [1] => Array ( [0] => 2 [1] => 4 ) 
                [2] => Array ( [0] => 3 [1] => 5 ) ) 
        */
    
        //Copied from http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array
        function in_array_r($needle, $haystack, $strict = false) {
            foreach ($haystack as $item) {
                if (($strict ? $item === $needle : $item == $needle) || (is_array($item) && in_array_r($needle, $item, $strict))) {
                    return true;
                }
            }
    
            return false;
        }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图