【问题标题】:How can I loop through an array, starting at an offset and looping round again?如何循环遍历数组,从偏移量开始并再次循环?
【发布时间】:2014-03-07 13:18:19
【问题描述】:

新手问题:我正在使用 foreach 循环从数组中获取项目。

我需要从一个偏移量开始循环——(我使用 $i 变量来执行此操作,没问题)。

但是当我的 foreach 到达数组的末尾时,我希望它再次开始遍历数组,直到它到达偏移量。

我需要这样做,以便我可以让用户打开艺术家作品集中的任何图像,并将此图像用作缩略图图标网格中呈现的第一张图像,所有其他图像随后填充网格的其余部分.

有什么想法吗? 请记住,我是 PHP 新手! :) 请参阅下面的我当前代码的示例...

       $i=0;
        $limit=50;// install this in the if conditional with the offset in it (below) to limit the number of thumbnails added to the page.
        $offset=$any_arbitrary_link_dependant_integer;

        foreach($portfolio_image_array as $k=>$image_obj){//$k = an integer counter, $image_obj = one of the many stored imageObject arrays.
            $i++;
            if ($i > $offset && $i < $limit) {// ignore all portfolio_array items below the offset number.
                if ($img_obj->boolean_test_thing===true) {// OK as a way to test equivalency?
                    // do something
                } else if ($img_obj->boolean_test_thing===false) { // Now add all the non-see_more small thumbnails:
                    // do something else
                } else {
                    // error handler will go here.
                }
            } // end of offset conditional

        }// end of add boolean_test_thing thumbnails foreach loop.
    };// end of add thumbnails loop.

【问题讨论】:

  • 您可以为此使用模除法:使用数字 for 循环而不是 foreach,并且不是按循环索引变量引用数组条目,而是按您的偏移量取模。
  • 然后使用for loop,它有自定义的开始和结束值

标签: php arrays loops


【解决方案1】:

一个简单的方法是使用两个单独的数字 for 循环,第一个从偏移到结束,第二个从开始到偏移。

<?php
// Create an example array - ignore this line
$example = array(1,2,3,4,5,6);

$offset = 3;

// Standard loop stuff
$count = count($example);
for($i = $offset; $i < $count; $i++)
{
    echo $example[$i]."<br />";
}

for($i = 0; $i < $offset; $i++)
{
    echo $example[$i]."<br />";
}

?>

这也几乎肯定比对数组中的每个元素进行多次检查要便宜,并且它准确地向其他查看此代码的程序员表达了您想要做的事情 - 包括 2 周后的您自己。

编辑:根据数组的性质,为了使用数字键,您可能首先需要执行$example = array_values($portfolio_image_array);

【讨论】:

  • 哇!感谢马克和马库斯的快速回复!今晚我回到办公桌前,我会试一试。
【解决方案2】:
$i = 0;
$limit = 50;
$offset = $any_arbitrary_link_dependant_integer;
$count = count($portfolio_image_array);
        foreach($portfolio_image_array as $k=>$image_obj){//$k = an integer counter, $image_obj = one of the many stored imageObject arrays.
        $i++;
        if ($i > $offset && $i < $limit && $i < ($count - $offset)) {// ignore all portfolio_array items below the offset number.
            if ($img_obj->boolean_test_thing===true) {// OK as a way to test equivalency?
                // do something
            } else if ($img_obj->boolean_test_thing===false) { // Now add all the non-see_more small thumbnails:
                // do something else
            } else {
                // error handler will go here.
            }
        } // end of offset conditional

    }// end of add boolean_test_thing thumbnails foreach loop.
};

我只添加了一个 $count 变量。

编辑:如果您的数组从 0 开始,我建议您将 $i++; 放在 foreach 循环的末尾。

【讨论】:

  • 哇!感谢马克和马库斯的快速回复!今晚我回到办公桌前,我会试一试。
  • 没问题。我想听听您的意见,哪种解决方案最适合您;)
  • 你好,马克,我已经尝试过您的解决方案,但我认为我也应该实施其他解决方案之一?我的数组从偏移量循环,但不会重新开始循环回到偏移量。我的测试页面在这里,如果有帮助的话:runninghead.com/portfolio.php?image_to_display=28 我有几个图标设置为在主页上使用不同的偏移量,用于测试。希望从 URL 中可以看出。请忽略图像的质量——它们只是多年前的占位符! :)
  • 我建议你使用 2 个单独的循环。
  • OK @Mark Walet 等人,还不确定如何在这个论坛上正确发帖,但这里可以。我得到的问题排序如下:非常感谢您的所有帮助! :)
【解决方案3】:

使用 Answer Question 来强制 StackOverflow 让我发布相当长的文本! 好的@Mark Walet 等人,还不确定如何在这个论坛上正确发帖,但是就这样吧。我得到的问题排序如下:

$i=0;
$offset=$image_to_display_number;
    $array_length = count($portfolio_image_array);

    // FIRST HALF LOOP:
    foreach($portfolio_image_array as $k=>$img_obj){// go through array from offset (chosen image) to end.
    if ($i >= $offset && $i <= $array_length) {
            echo write_thumbnails_fun($type_of_thumbnail, $image_path, $k, $i, $portfolio_image_array, $title, $image_original);
            $t_total++;// update thumbnail total count.
        }
        $i++;
    }// end of foreach loop 1.
    $looped=true;// Just FYI.
    $i=0;// Reset.
    // SECOND HALF LOOP:
    foreach($portfolio_image_array as $k=>$img_obj){// go through array from beginning to offset.
        if ($i < $offset) {
            echo write_thumbnails_fun($type_of_thumbnail, $image_path, $k, $i, $portfolio_image_array, $title, $image_original);
        }
        $i++;
    }// end of foreach loop 2.

非常感谢您的所有帮助! :)

【讨论】:

    【解决方案4】:

    @arkascha 建议使用模运算符

    <?php
    
    $example = array(1,2,3,4,5,6);
    $count = count($example);
    $offset = 3;
    
    for($i = 0; $i < $count; $i++) {
      $idx = ($offset + $i) % count 
      echo $example[$idx]."<br />";
    }
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-30
      • 2013-11-15
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 2020-07-28
      • 1970-01-01
      相关资源
      最近更新 更多