Just group the D’s first and dont hardcode the indices, use $i
$S = array();
$D = array();
$M = array("1"=>30,"2"=>31,"3"=>30);
$Y = array("1"=>360,"2"=>360,"3"=>360);
$O = 30000;
$P = 0.3;
$N = 10509.74;
for($i = 1, $size = count($M); $i <= $size; $i++){
$final_D = 0;
// group the D's first (O-D1), (O-D1-D2), ... and so on
$temp = array_slice($D, 0, $i);
foreach ($temp as $key => $value) {
$final_D += $value;
}
$S[$i] = ($O-$final_D)*$P*$M[$i]/$Y[$i];
$D[$i] = $N - $S[$i];
}
print_r($S); // Array ( [1] => 750 [2] => 522.87338333333 [3] => 256.33483458333 )
echo '<br/>';
print_r($D); // Array ( [1] => 9759.74 [2] => 9986.8666166667 [3] => 10253.405165417 )
0
solved How to solve this mathematical equation in php [closed]