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

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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波