Get value of previous array key in PHP
<p>I am trying to do a Drug Half life calculator with PHP.  I want to pass in the amount of the drug taken per day in MG's and pass in the Half-life hours, then it will calculate how much of the drug is left after X amount of time and how much is still left from previous doses.</p>

<p>So far this is what I have...</p>

<pre><code>function calcHalfLife( $mgTaken , $drugHalfLifeHours , $day = 1 ) {
    //total number of half-lifes elapsed
    $total_half_lifes = ($day * 24) / $drugHalfLifeHours;
    //total reduction in dosage
    $reductionFactor = pow( 0.5 , $total_half_lifes );
    //return the current dosage in the person's system
    return round( $mgTaken * $reductionFactor , 8 );
}
</code></pre>

<p>Then I am working on this function below which will let me pass in an Array of Days and the MG taken for each day, the function should then iterate the array and run the function above on each day's value.</p>

<pre><code>function HalfLifeChart(array $days, $drugHalfLifeHours ) {
    $out = array();
    foreach ($days as $day =&gt; $dosage) {
        $out[$day] = calcHalfLife( $dosage , $drugHalfLifeHours , 1 );
    }
    return $out;
}
</code></pre>

<p>Example usage...  </p>

<pre><code>$day = array(1 =&gt; 30,
             2 =&gt; 0,
             3 =&gt; 0,
             4 =&gt; 40,
             5 =&gt; 30,
             6 =&gt; 10,
             7 =&gt; 60);

echo '&lt;br&gt;&lt;pre&gt;';
print_r(HalfLifeChart( $day, 4.5));
echo '&lt;/pre&gt;&lt;br&gt;&lt;br&gt;';
</code></pre>

<p>Now I have a pretty good start but the <code>HalfLifeChart</code> function is where I need to do more work, right now it will run the Half-life calculations on the number passed for each day which is good, but I need to get the result from the previous day and add that to the MG taken on the current day and then run the Calculations on that number.  </p>

<p>So for example, if I have 0.8043mg left from the previous day and I took 30mg today, then the calculation should be ran on 0.8043 + 30 and then pass that result through my Half life calculator function.  </p>

<p>I am not sure how to grab the result from the previous day though, any help please?</p>
