title: PHP Looping through JSON data without knowing the numbers of data
body: <p>I am trying to get all images from an image API. It returns a maximum of 500 result at a time and if the result has a <code>next_page</code> field, then I have to grab the value of that field and add it to the URL. The code should continue looping until that field is absent. I used the following code to grab the first two pages:</p>
  <pre class="lang-php prettyprint-override"><code>$key = true;
  $link = 'https://url.com/dhj/?prefix=images&amp;type=download';
  $json = file_get_contents($link);
  $data = json_decode($json, true);
  $dataArray = array();
  foreach ($data[&quot;images&quot;] as $r)
  {
      array_push($dataArray, array($r[&quot;id&quot;], $r[&quot;image&quot;]));
  }
  while($key)
  {
      if($data[&quot;next_page&quot;])
      {
          $key=true;
          $link2 = &quot;https://url.com/dhj/?prefix=images&amp;type=download&amp;next_page=&quot; . $data[$next_page];
          $json2 = file_get_contents($link2);
          $data2 = json_decode($json2, true);
          foreach ($data2[&quot;images&quot;] as $r2)
          {
              array_push($dataArray, array($r2[&quot;id&quot;], $r2[&quot;image&quot;]));
          }
      }
      else
      {
          $key=false;
      }
  }
  </code></pre>
  <p>This should fetch 2000 records but is only fetching 1000 records, so it appears the loop is not working as expected.</p>