【发布时间】: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,它有自定义的开始和结束值