【问题标题】:How to reverse PHP array after each time it loops每次循环后如何反转PHP数组
【发布时间】:2013-07-16 13:30:40
【问题描述】:

如何在 PHP 中进行蛇形循环或每次循环后如何反转 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>";

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

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

?>

输出:

Round 1
Team 1
Team 2
Team 3
Team 4

Round 2
Team 4
Team 3
Team 2
Team 1

Round 3
Team 1
Team 2
Team 3
Team 4

Round 4
Team 4
Team 3
Team 2
Team 1

基本上,如果$round 是奇数,您可以看到数组对ascending 进行排序,如果是偶数,则对descending 进行排序。

【问题讨论】:

  • $teams = array_reverse($teams);
  • 是的,我之前尝试过,但似乎没有用。我想我把它放在循环的错误端口中,因为这似乎现在可以工作了。

标签: php arrays sorting loops for-loop


【解决方案1】:

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

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];
}

【讨论】:

  • 奇偶交替与array_reverse有何不同?
  • @bigmike7801 我更新了我的答案,你仍然需要交替奇数/偶数,只是在一个你使用普通索引,而在另一个你反转它(计算位置比重新创建任何数组要好得多倒序)
  • 好的,我明白你在说什么。基本上一直只有 2 个版本的数组,而不是每次都反转它。
  • @bigmike7801 是的,但不是数组的两个版本,只是索引的两个版本(这是一个很容易计算的小数字,可以告诉您将从数组中获取哪些项目)
  • myindex = ((i - 1) - i); 你当然是想写myindex = ((len - 1) - i);
【解决方案2】:

修改你的代码来实现array_reverse:

<?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 />";
  }
}
?>

【讨论】:

    【解决方案3】:

    使用php的array_reverse函数。

    <?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);
    }
    
    ?> 
    

    【讨论】:

      【解决方案4】:

      array_reverse 是返回数组反转的函数。

      如果您试图让 php 数组对象在每个循环中都具有反转的内容,那么您需要每次都设置数组变量;否则,您可以简单地在每个循环中返回 array_reverse 的输出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-04-24
        • 2013-05-31
        • 1970-01-01
        • 2016-04-09
        • 2017-04-28
        • 1970-01-01
        相关资源
        最近更新 更多