<?php
$data[2020] = array_fill_keys(range(1,12), '1,000');
$data[2021] = array_fill_keys(range(1,8), '1,500');
$data[2021][9] = null;
$data[2021][10] = null;
$data[2021][11] = null;
$data[2021][12] = null;
$thisYear = $year ?? date('Y');
$lastYear = $thisYear - 1;
$thisYearRunningTotal = 0;
$lastYearRunningTotal = 0;
$result = [];
for ($month = 1; $month <= 12; ++$month) {
$thisYearMonthTotal = $data[$thisYear][$month]; // from model
$thisYearRunningTotal = $thisYearMonthTotal === null
? null
: (int)str_replace(',', '', $thisYearMonthTotal) + $thisYearRunningTotal;
$lastYearMonthTotal = $data[$lastYear][$month]; // from model
$lastYearRunningTotal = $lastYearMonthTotal === null
? null
: (int)str_replace(',', '', $lastYearMonthTotal) + ($lastYearRunningTotal ?? 0);
$result[] = [
'month' => $month,
'total_now' => $thisYearRunningTotal,
'total_before' => $lastYearRunningTotal,
];
}
var_export($result);
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.4, 8.3.6 - 8.3.7
- array (
0 =>
array (
'month' => 1,
'total_now' => 1500,
'total_before' => 1000,
),
1 =>
array (
'month' => 2,
'total_now' => 3000,
'total_before' => 2000,
),
2 =>
array (
'month' => 3,
'total_now' => 4500,
'total_before' => 3000,
),
3 =>
array (
'month' => 4,
'total_now' => 6000,
'total_before' => 4000,
),
4 =>
array (
'month' => 5,
'total_now' => 7500,
'total_before' => 5000,
),
5 =>
array (
'month' => 6,
'total_now' => 9000,
'total_before' => 6000,
),
6 =>
array (
'month' => 7,
'total_now' => 10500,
'total_before' => 7000,
),
7 =>
array (
'month' => 8,
'total_now' => 12000,
'total_before' => 8000,
),
8 =>
array (
'month' => 9,
'total_now' => NULL,
'total_before' => 9000,
),
9 =>
array (
'month' => 10,
'total_now' => NULL,
'total_before' => 10000,
),
10 =>
array (
'month' => 11,
'total_now' => NULL,
'total_before' => 11000,
),
11 =>
array (
'month' => 12,
'total_now' => NULL,
'total_before' => 12000,
),
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array (
0 =>
array (
'month' => 1,
'total_now' => 1500,
'total_before' => 1000,
),
1 =>
array (
'month' => 2,
'total_now' => 3000,
'total_before' => 2000,
),
2 =>
array (
'month' => 3,
'total_now' => 4500,
'total_before' => 3000,
),
3 =>
array (
'month' => 4,
'total_now' => 6000,
'total_before' => 4000,
),
4 =>
array (
'month' => 5,
'total_now' => 7500,
'total_before' => 5000,
),
5 =>
array (
'month' => 6,
'total_now' => 9000,
'total_before' => 6000,
),
6 =>
array (
'month' => 7,
'total_now' => 10500,
'total_before' => 7000,
),
7 =>
array (
'month' => 8,
'total_now' => 12000,
'total_before' => 8000,
),
8 =>
array (
'month' => 9,
'total_now' => NULL,
'total_before' => 9000,
),
9 =>
array (
'month' => 10,
'total_now' => NULL,
'total_before' => 10000,
),
10 =>
array (
'month' => 11,
'total_now' => NULL,
'total_before' => 11000,
),
11 =>
array (
'month' => 12,
'total_now' => NULL,
'total_before' => 12000,
),
)
preferences:
175.1 ms | 404 KiB | 158 Q