简体   繁体   中英

Get value of previous array key in PHP

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.

So far this is what I have...

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 );
}

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.

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

Example usage...

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

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

Now I have a pretty good start but the HalfLifeChart 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.

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.

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

Why don't you store the result of the previous day on another variable?

Something like:

function HalfLifeChart(array $days, $drugHalfLifeHours ) {
    $out = array();
    $prevDay = 0;
    foreach ($days as $k => $v) {
        $out[$k] = calcHalfLife( $v , $drugHalfLifeHours , 1 ); //change this
        $prevDay = $out[$k];
    }
    return $out;
}

Just store it.

function HalfLifeChart(array $days, $drugHalfLifeHours ) {
    $out = array();
    $yesterday = 0;
    foreach ($days as $k => $v) {
        $out[$k] = calcHalfLife($v + $yesterday, $drugHalfLifeHours, 1);
        $yesterday = $out[$k];
    }
    return $out;
}
function HalfLifeChart(array $days, $drugHalfLifeHours ) {
  $out=array();
  $remains=0;
  foreach ($days as $day => $dosage) {
    $total=$remains+$dosage;
    $out[$day]=$total;
    $remains=calcHalfLife( $total , $drugHalfLifeHours , 1 );
  }
  return $out;
}

gives you

print_r(HalfLifeChart( $day, 4.5));
Array
(
    [1] => 30
    [2] => 0.74409424
    [3] => 0.01845587
    [4] => 40.00045776
    [5] => 30.99213701
    [6] => 10.76870236
    [7] => 60.26709765
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM