dongtiao5094
2016-12-29 07:24
浏览 183
已采纳

php如何获取数组中的下一个元素并在结束时再次循环?

How do i get the next element of the array and loop it again when it comes to the last element?

Problem: Unable to apply the array to the url and let it goes to the next item in the array based on the Item Code.

PS: missing 019

Array
(
    [0] => 001
    [1] => 002
    [2] => 003
    [3] => 004
    [4] => 005
    [5] => 006
    [6] => 007
    [7] => 008
    [8] => 009
    [9] => 010
    [10] => 011
    [11] => 012
    [12] => 013
    [13] => 014
    [14] => 015
    [15] => 016
    [16] => 017
    [17] => 018
    [18] => 020
)

$itemCode = isset($_GET["itemCode"]) ? $_GET["itemCode"] : "001";
$catCode = isset($_GET["cat"]) ? $_GET["cat"] : "ac";
foreach ($productArr[$catCode] as $imgNumber => $productDetail) {
            array_push($arr, $imgNumber);
            $imgNumber = $arr;
            // index[18] change to 020
        }
$itemCode = $arr; // my itemCode will be the $arr now

for ($i = 0; $i <= count($productArr[$catCode]); $i++) {
            $prevItem = SOME FUNCTION ; //get prev array
            $nextItem = SOME FUNCTION ; // get next array      

            if ($itemCode > count($arr) || $itemCode < "001") {
                $itemCode = "001";
            }
        }
echo"<a href='http://localhost/collectionDetail.php?cat={$catCode}&itemCode=" . sprintf("%03d", $prevItem) . "' ><img src='images/arrow_left.jpg'> </a>";
echo"<a href='http://localhost/collectionDetail.php?cat={$catCode}&itemCode=" . sprintf("%03d", $nextItem) . "' ><img src='images/arrow_right.jpg'> </a>";
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题