I am trying to get previous value for current loop for some calculations. As a starting point lets describe what does the code.
- Outer loop generates month names
- $forecast_correction is a loop for correcting some values
The idea is to make a forecast/prediction for the months having the base cost and default percentage for the months.
This is the logic for the loop. January is the starting month, we set there the base $price but next month( February ) should be January * $default_percent,
March should be February * $default_percent etc. all should include corrections.
When it comes to the corrections we got two variables
$percentage = (!empty($modified_percentage)) ? $modified_percentage : $default_percent;
$month_cost = $price * $percentage + $correction_cost;
Percentage can change if outer loop month($m) equals to correction array $month, else its the default value.
Month cost is a basic calculation.
+----------+---------------+------------+------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+
| jan | feb | mar | apr | may | june | july | augu | sept | oct | nov | dece |
+----------+---------------+------------+------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+
| 41290.65 | 41290.65*1.03 | feb * 1.03 | mar * 1.03 | apr * 1.03 | may * 1.03 | june * 1.03 | july * 1.03 | augu * 1.03 | sept * 1.03 | oct * 1.03 | nov * 1.03 |
+----------+---------------+------------+------------+------------+------------+-------------+-------------+-------------+-------------+------------+------------+
Code snippet
<?PHP
$forecast_correction = array(
array(
'month' => '2',
'add' => 150.00,
'substract' => 0.00,
'final' => 150.00,
'percent' => 1.02
),
array(
'month' => '3',
'add' => 0.00,
'substract' => 250.00,
'final' => -250.00,
'percent' => NULL
),
array(
'month' => '4',
'add' => 0.00,
'substract' => 0.00,
'final' => 0.00,
'percent' => 0.15
)
);
$price = 41290.65;
$default_percent = 1.03;
for ($m = 1; $m <= 12; ++$m) {
$correction_cost = 0;
$modified_percentage = null;
foreach ($forecast_correction as $correction) {
if ($m == $correction['month']) {
$correction_cost = $correction['final'];
if (!empty($correction['percent'])) {
$modified_percentage = $correction['percent'];
}
}
}
;
$percentage = (!empty($modified_percentage)) ? $modified_percentage : $default_percent;
$month_cost = $price * $percentage + $correction_cost;
echo date('F', mktime(0, 0, 0, $m, 1)) . " :: $month_cost :: " . $correction_cost . " :: " . $percentage . "% \n";
}
Question and desired output
Question : How to implement the February = January * percentage logic in this particular example to have all the results properly calculated with the correction array.
Desired output ( can be plain text, array whatever. )
Month : Cost
If you need future clarification don't hesitate to ask, I am quite stuck with this one.
MCVE sandbox
http://sandbox.onlinephpfunctions.com/code/0c705ab721c6160813b98d133d8a1b7ff876cdca