如何在每次循环后反转PHP数组

如何在每次循环后反转PHP数组,php,arrays,sorting,loops,for-loop,Php,Arrays,Sorting,Loops,For Loop,如何在PHP中执行snake循环,或者如何在每次循环后反转PHP数组 我不确定这个方法或技术叫什么,所以我将把它称为snake循环 基本上,我要做的是循环一个数组,然后在下一次循环时反转该数组的顺序 我已经想出了一个看似简单的方法,但我只是不确定这是否是正确的技术,或者是否有更好的方法 <?php $rounds = 4; $teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4') ; for($round = 1; $round <

如何在PHP中执行snake循环,或者如何在每次循环后反转PHP数组 我不确定这个方法或技术叫什么,所以我将把它称为snake循环

基本上,我要做的是循环一个数组,然后在下一次循环时反转该数组的顺序

我已经想出了一个看似简单的方法,但我只是不确定这是否是正确的技术,或者是否有更好的方法

<?php
$rounds = 4;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4') ;

for($round = 1; $round <= $rounds; $round++){
    echo "<h1>Round $round</h1>";

    if ($round % 2 == 0) {
        krsort($teams);
    }else{
        asort($teams);
    }        

    foreach($teams as $team){
        echo "$team<br />";
    }
}

?>

基本上,您可以看到,如果
$round
是奇数,则数组按
升序
排序;如果是偶数,则按
降序
排序

我认为反转数组很昂贵,我认为最好是计算反转索引:

array A (6 length) 0,1,2,3,4,5

array B (5 length) 0,1,2,3,4

(len-1)-i
//^ this should calculate the inverted index, examples:

//in the array A, if you are index 3: (6-1)-3 = 2, so 3 turns to 2
//in the array A, if you are index 1: (6-1)-1 = 4, so 1 turns to 4
//in the array B, if you are index 3: (5-1)-3 = 1, so 3 turns to 1
//in the array B, if you are index 1: (5-1)-1 = 3, so 1 turns to 3
我不写PHP,但应该是这样的:

teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4');
len = teams.length;
myindex; //initializing the var

for(i=0; i<len; i++){
    echo "<h1>Round "+ (i+1) +"</h1>";
    myindex = i;

    if(i%2 == 0) {
        myindex = ((len-1) - i);
    }

    echo team[myindex];
}
teams=array('team1'、'team2'、'team3'、'team4');
len=团队长度;
我的指数//初始化变量

对于(i=0;i我认为反转数组很昂贵,我认为计算反转指数更好:

array A (6 length) 0,1,2,3,4,5

array B (5 length) 0,1,2,3,4

(len-1)-i
//^ this should calculate the inverted index, examples:

//in the array A, if you are index 3: (6-1)-3 = 2, so 3 turns to 2
//in the array A, if you are index 1: (6-1)-1 = 4, so 1 turns to 4
//in the array B, if you are index 3: (5-1)-3 = 1, so 3 turns to 1
//in the array B, if you are index 1: (5-1)-1 = 3, so 1 turns to 3
我不写PHP,但应该是这样的:

teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4');
len = teams.length;
myindex; //initializing the var

for(i=0; i<len; i++){
    echo "<h1>Round "+ (i+1) +"</h1>";
    myindex = i;

    if(i%2 == 0) {
        myindex = ((len-1) - i);
    }

    echo team[myindex];
}
teams=array('team1'、'team2'、'team3'、'team4');
len=团队长度;
myindex;//初始化变量
for(i=0;i是返回数组相反方向的函数

如果您试图让php数组对象在每个循环中都有反向内容,那么每次都需要设置数组变量;否则,您只需在每个循环中返回array\u reverse的输出。

是返回数组反向内容的函数

如果您试图让php数组对象在每个循环中都有反向内容,那么每次都需要设置数组变量;否则,您只需在每个循环中返回array\u reverse的输出。

使用php函数

<?php
$rounds = 4;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4') ;

for($round = 1; $round <= $rounds; $round++){
    echo "<h1>Round $round</h1>";
    echo implode("<br/>", $teams);
    $teams = array_reverse($teams);
}

?> 

使用php函数

<?php
$rounds = 4;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4') ;

for($round = 1; $round <= $rounds; $round++){
    echo "<h1>Round $round</h1>";
    echo implode("<br/>", $teams);
    $teams = array_reverse($teams);
}

?> 

修改代码以实现数组\u反转:

<?php
$rounds = 4;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4') ;

for($round = 1; $round <= $rounds; $round++){
  echo "<h1>Round $round</h1>";

  if ($round % 2 == 0) {
    $teams = array_reverse($teams);
  }    
  foreach($teams as $team){
    echo "$team<br />";
  }
}
?>

修改代码以实现数组\u反转:

<?php
$rounds = 4;
$teams = array('Team 1', 'Team 2', 'Team 3', 'Team 4') ;

for($round = 1; $round <= $rounds; $round++){
  echo "<h1>Round $round</h1>";

  if ($round % 2 == 0) {
    $teams = array_reverse($teams);
  }    
  foreach($teams as $team){
    echo "$team<br />";
  }
}
?>


$teams=array\u reverse($teams);
是的,我之前试过,但似乎不起作用。我想我把它放在了错误的循环端口中,因为它现在似乎起作用了。
$teams=array\u reverse($teams)
是的,我之前试过,但似乎不起作用。我想我把它放在了循环的错误端口中,因为它现在似乎起作用了。交替奇数/偶数与
数组_reverse
有何不同?@bigmike7801我更新了我的答案,你仍然需要交替奇数/偶数,在一个循环中使用正常索引,在另一个循环中使用正常索引r你反转它(计算位置比按相反顺序重新创建任何数组要好得多)好的,我明白你的意思。基本上,任何时候都只有两个版本的数组,而不是每次都反转。@bigmike7801是的,但不是两个版本的数组,只有两个版本的索引(这是一个很容易计算的小数字,可以告诉您将从数组中提取哪些项)
myindex=((i-1)-i);
您当然想写
myindex=((len-1)-i)
?交替奇偶与数组_reverse有何不同?@bigmike7801我更新了我的答案,你仍然需要交替奇偶,一个是使用正常索引,另一个是反转索引(计算位置远比按相反顺序重新创建任何数组好得多)好的,我明白你的意思了。基本上,数组在任何时候都只有两个版本,而不是每次都反转。@bigmike7801是的,但不是两个版本的数组,只有两个版本的索引(这是一个很容易计算的数字,可以告诉我们从数组中提取什么项)
myindex=((I-1)-I)
当然你是想写
myindex=((len-1)-i);