title: php how to get next element in the array and loop again when ended?
body: <p>How do i get the next element of the array and loop it again when it comes to the last element?</p>
  
  <p>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.</p>
  
  <p><strong>PS: missing 019</strong></p>
  
  <pre><code>Array
  (
      [0] =&gt; 001
      [1] =&gt; 002
      [2] =&gt; 003
      [3] =&gt; 004
      [4] =&gt; 005
      [5] =&gt; 006
      [6] =&gt; 007
      [7] =&gt; 008
      [8] =&gt; 009
      [9] =&gt; 010
      [10] =&gt; 011
      [11] =&gt; 012
      [12] =&gt; 013
      [13] =&gt; 014
      [14] =&gt; 015
      [15] =&gt; 016
      [16] =&gt; 017
      [17] =&gt; 018
      [18] =&gt; 020
  )
  
  $itemCode = isset($_GET["itemCode"]) ? $_GET["itemCode"] : "001";
  $catCode = isset($_GET["cat"]) ? $_GET["cat"] : "ac";
  foreach ($productArr[$catCode] as $imgNumber =&gt; $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 &lt;= count($productArr[$catCode]); $i++) {
              $prevItem = SOME FUNCTION ; //get prev array
              $nextItem = SOME FUNCTION ; // get next array      
  
              if ($itemCode &gt; count($arr) || $itemCode &lt; "001") {
                  $itemCode = "001";
              }
          }
  echo"&lt;a href='http://localhost/collectionDetail.php?cat={$catCode}&amp;itemCode=" . sprintf("%03d", $prevItem) . "' &gt;&lt;img src='images/arrow_left.jpg'&gt; &lt;/a&gt;";
  echo"&lt;a href='http://localhost/collectionDetail.php?cat={$catCode}&amp;itemCode=" . sprintf("%03d", $nextItem) . "' &gt;&lt;img src='images/arrow_right.jpg'&gt; &lt;/a&gt;";
  </code></pre>