title: PHP foreach loop where two values are compared
body: <p>I'm building a simple weight log, where there'a table that display the user's progress. I'm using Codeigniter.</p>
  
  <pre><code>    &lt;?php foreach($logs as $log) : ?&gt;
          &lt;tr&gt;
          &lt;td&gt;&lt;?php echo $log-&gt;date ;?&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo $log-&gt;weight ;?&gt; kg.&lt;/td&gt;
          &lt;td&gt;&lt;!-- variation between this record and previous record. weigth[a] - weight[b] --&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;?php echo anchor('logs/edit/'.$log-&gt;id, 'Editar'); ?&gt; &lt;?php echo anchor('logs/delete/'.$log-&gt;id, 'Delete'); ?&gt;&lt;/td&gt;
          &lt;/tr&gt;
      &lt;?php endforeach ;?&gt;
  </code></pre>
  
  <p>I'm trying to calculate the variation between the first row and the second row, so as to get the weight loss or gain between logs. I was wondering how to access the loop's previous record so as to substract it from the current record's weight.</p>
  
  <pre><code>---------------------------------------
    DATE     |   WEIGHT  |   VARIATION
  ---------------------------------------
    Nov 20   |    70 kg  |     -1 kg      &lt;&lt; LAST LOGGED WEIGHT, FIRST IN ARRAY
  .......................................
    Nov 15   |    71 kg  |      -
  ---------------------------------------
  </code></pre>