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

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条回答 默认 最新

  • dongxing1965 2016-12-29 07:28
    已采纳

    There are multiple functions to help you navigate through arrays.

    A very usefull one is the continue; statement which will go to the next loop.

    $array = array(1, 2, 3, 4, 5);
    foreach ($array as $value) {
        if ($value == '3') {
            continue;
        }
        echo $value."
    ";
    }
    

    Outputs:

    1
    2
    4
    5
    

    In my example I use a foreach loop to loop through the array. In most occasions this is a lot easier and less troublesome than using for.

    Update

    Upon consideration, If you want to get the previous and next item in your array using a foreach, take a look at the following:

    $itemCode = '003';
    
    $array = array(
        '001', '002', '003',
        '004', '005', '006',
        '007', '008', '009',
    );
    $iteration = $previous = $next = NULL;
    foreach($array as $value) {
        $iteration = $value;
        if ($iteration === $itemCode) {
           $current = $iteration;
           continue;
        }
        if ($current !== NULL) {
            $next = $iteration;
            break;
        }
        $previous = $value;
    
    }
    
    var_dump($previous, $current, $next);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • doudi8231 2016-12-29 07:31

    If you want to continue look again when it's over then try below.

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

    After over your array we can reset the value of $i and then again it will start again .

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题