简体   繁体   English

PHP数组元素交叉

[英]PHP Array Elements Cross

I have one array contains number of teams. 我有一个array包含多个团队。 I want each team to play with all other teams. 我希望每个团队都与其他所有团队一起比赛。

I have tried to do it with for loop but it nothing works. 我试图用for loop来做,但是没有用。

the array is like this. 数组是这样的。

Array ( [0] => 2 [1] => 3 [2] => 8 [3] => 9 [4] => 11 [5] => 12 )

I want to do it like this. 我想这样

[0] - [1]
[0] - [2]
[0] - [3]
[0] - [4]
[0] - [5]

[1] - [2]
[1] - [3]
[1] - [4]
[1] - [5]

[2] - [3]
[2] - [4]
[2] - [5]

[3] - [4]
[3] - [5]

[4] - [5]

my code was like this 我的代码是这样的

    function createMatchesStandings($teams,$homeaway,$round)
    {
        include_once('class_match.php');

        if($homeaway == 0)
        {
            // one way matches
            $numberOfMatches = count($teams) - 1;
            for($i = 0; $i<=$numberOfMatches;$i++)
            {
                $match = new Match();
                $match->standing = $this->id;
                $match->round = $round;
                $match->home_team = $teams[$i];
                $match->away_team = $teams[$i+1];
                $match->week = $i;
                $match->date = '0000-00-00';
                $match->insert();
            }


        }elseif($homeaway == 1)
        {
            // home away matches ($teams * 2) - 2

        }

    }

Can something like to code bellow help you? 下面的代码可以帮助您吗? (I hope I understood the question.) (我希望我能理解这个问题。)

$teams=array(1,2,3,4,5);

for($i=0;$i<sizeof($teams);$i++)
  for($j=$i+1;$j<sizeof($teams);$j++)
    echo $teams[$i].' - '.$teams[$j].'<br />';

what is your name of the array and i can say you to use foreach loop as 你叫什么名字的数组,我可以说你用foreach循环作为

foreach(array["id"] as $key=>$value){
 $team[$id]=$value;// got new aaray as your team
 //now make your code for match between team[id] and array[id] 

   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM