title: Using foreach in a while loop
body: <p>Problem: I have a foreach loop in a while loop. for each row selected from the database the foreach loop has to done. but when I echo $row it seems like it immedieatly puts out all the rows and then afterwards goes trough the foreach just once. I've read through the manuals but it did not help me unfortunately. </p>
  
  <p>Code:</p>
  
  <pre><code>$conn = oci_connect('login', 'pass', '127.0.0.1/xe');
  $stid = oci_parse($conn, 'select TOKEN, SECRET from TOKENS');
  oci_execute($stid);
  
  $time=20:00
  
  while(($row = oci_fetch_row($stid)) != false) {
      $fitbit-&gt;setOAuthDetails($row[0], $row[1]);
      //retreive steps. (with date and time)
      $steps = $fitbit-&gt;getTimeSeries('steps', '2015-06-01', '2015-07-01');
      $n=0;
      foreach ($steps as $value){
          $sqlArray[$n]['dateTime']=$value-&gt;dateTime;
          $sqlArray[$n]['time'] = $time;
          $sqlArray[$n]['steps'] = $value-&gt;value;
          $n++;
      }
  
  }
  </code></pre>
  
  <p>It's probably me overlooking something but I hope my question can be answered.</p>
  
  <p>Updated code:</p>
  
  <pre><code>$time="12:00";
  $n=0;
  $x=0;
  while(($row = oci_fetch_row($stid)) != false) {
  
      $sqlArray[$n]['token'] = $row[0];
      $sqlArray[$n]['secret'] = $row[1];
      echo 'hoi';
      $fitbit-&gt;setOAuthDetails($sqlArray[$n]['token'], $sqlArray[$n]['secret']);
      //retreive steps. (with date and time)
      $steps = $fitbit-&gt;getTimeSeries('steps', '2015-06-01', '2015-07-01');
      foreach ($steps as $value){
          $sqlArray[$n][$x]['dateTime']=$value-&gt;dateTime;
          $sqlArray[$n][$x]['time'] = $time;
          $sqlArray[$n][$x]['steps'] = $value-&gt;value;
          $x++;
      }   
      //retreive calories and add to sqlArray where date matches. (used for testing now, when acces to intraday api the outcommented code below will be used. 
      $calories = $fitbit-&gt;getTimeSeries('caloriesOut', '2015-06-01', '2015-07-01');
      $x=0;
      foreach ($calories as $value){  
          $checkdate=$value-&gt;dateTime;
          if ($sqlArray[$n][$x]['dateTime'] == $checkdate){
              $sqlArray[$n][$x]['calories'] = $value-&gt;value;
          }
          $x++;
  
      }
  $n++;
  };
  </code></pre>
  
  <p>Now my output is as follows: </p>
  
  <pre><code>Array
  (
      [0] =&gt; Array
          (
              [token] =&gt; token
              [secret] =&gt; secret
              [0] =&gt; Array
                  (
                      [dateTime] =&gt; 2015-06-01
                      [time] =&gt; 12:00
                      [steps] =&gt; 8046
                      [calories] =&gt; 2785
                  )
  </code></pre>
  
  <p>Which iteraties nicely over the tokens from the database. Though, After the first iteration the calories are dropped: </p>
  
  <pre><code> [1] =&gt; Array
          (
              [token] =&gt; token
              [secret] =&gt; secret
              [31] =&gt; Array
                  (
                      [dateTime] =&gt; 2015-06-01
                      [time] =&gt; 12:00
                      [steps] =&gt; 8046
                  )
  </code></pre>
  
  <p>I guess I'm doing something wrong again... I think it has to do with the $x variable, but I am not sure.   </p>
  
  <p>Yep it's me! I placed $x=0; in the while loop now and it works good! 
  Thanks for the help!            </p>