title: Foreach loop inside while loop - it never ends
body: <p>So, I have one curl API call which works fine when I do foreach outside the while loop. Once I move the foreach inside (because I need the values inside) it becomes an infinity loop.</p>
  <p>This is the setup</p>
  <pre><code>$query = &quot;SELECT id, vote FROM `administrators` WHERE type = 'approved'&quot;;
  $result = $DB-&gt;query($query);
  
  $offset = 0;
  $length = 5000;
      
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
                      
  do {
    curl_setopt($ch, CURLOPT_URL, &quot;https://api.gov/data?api_key=xxxxxxxxxx&amp;start=1960&amp;sort[0][direction]=desc&amp;offset=$offset&amp;length=$length&quot;);
      
    $jsonData = curl_exec($ch);
    $response = json_decode($jsonData);
                  
    foreach($response-&gt;response-&gt;data as $finalData){
        $allData[] = $finalData; 
    }
      
    $offset += count($response-&gt;response-&gt;data);
      
  } while ( count($response-&gt;response-&gt;data) &gt; 0 );
      
  curl_close($ch); 
  
  while($row = $DB-&gt;fetch_object($result)) {
  
     foreach ( $allData as $key =&gt; $finalData1 ) {
           
          // rest of the code
     }
  }
  </code></pre>
  <p>Once I run the page it goes infinity or until my browser crash. If I move <code>foreach ( $allData as $key =&gt; $finalData1 ) { }</code> outside the <code>while(){}</code> there is no such problem.</p>
  <p>Any ideas on what can be the problem here?</p>
  <p>UPDATE: <code>// rest of the code</code></p>
  <pre><code>$dataValue = str_replace(array(&quot;--&quot;,&quot;(s)&quot;,&quot;NA&quot;),&quot;NULL&quot;,$finalData1-&gt;value);
  if($frequency == &quot;dayly&quot;) {
      if($dataValue) {
          $query = &quot;UPDATE table SET $data_field = $dataValue WHERE year = $finalData1-&gt;period AND id = $row-&gt;id LIMIT 1&quot;;
      }
  }
  
  if(isset($query))
  $DB-&gt;query($query);
  unset($query);
  </code></pre>