循环时累计每月总计

Accumulate monthly totals while looping

我有一组数据,其中包含当年和上一年的每月金额。前一年总是有它的所有金额,但是当迭代当年的 current/future 个月时,它有一个 null 值。这是一个例子:

[
    {
        "month": 1,
        "total_now": "3,000"
        "total_before": "2,000"
    },
    {
        "month": 2,
        "total_now": "1,000"
        "total_before": "3,000"
    },
    {
        "month": 3,
        "total_now": null 
        "total_before": "1,000"
    },
    ...
    {
        "month": 12,
        "total_now": null,
        "total_before": "20,422"
    },
]

这是

public function loop_get(){
    $thn_now = date('Y'); 
    $thn_before = date('Y') -1; 
    $result = [];
    for ($month = 1; $month <= 12; $month++) {
        $result[] = [
            'month' => $month,
            'total_now' => $this->model->dbget($thn_now ,$month)->row()->total?? null
            'total_before' => $this->model->dbget($thn_before,$month )->row()->total?? null,
        ];
    }
}

如何将上个月的金额滚动到当月以保持每年的 运行 月度总额?此外,一旦一个月 returns 达到 null 金额,所有之前的金额都应该被删除,并且该月份应该显示 null 一年中的剩余时间。

期望的输出(假设当前月份是二月):

[
    {
        "month": 1,
        "total_now": "3,000"
        "total_before": "2,000" 
    },
    {
        "month": 2,
        "total_now": "1,000" // arr[0] + arr[1] = value 4000
        "total_before": "3,000" // arr[0] + arr[1] = value 5000
    },
    {
        "month": 3,
        "total_now": null 
        "total_before": "1,000" // arr[0] + arr[1] + arr[2] = value 6000
    },
    ...
    {
        "month": 12,
        "total_now": null,
        "total_before": "20,422" // arr[0] + arr[1] + arr[2] ..... = 20000
    },
]

看来你想要它像 ----

$array = [
            [
                "month" => 1,
                "total_now" => "3,000",
                "total_before" => "2,000"
            ],
            [
                "month" => 2,
                "total_now" => "1,000", // arr[0] + arr[1] = value 4000
                "total_before" => "3,000" // arr[0] + arr[1] = value 5000
            ],
            [
                "month" => 3,
                "total_now" => null,
                "total_before" => "1,000" // arr[0] + arr[1] + arr[2] = value 6000
            ],
            [
                "month" => 12,
                "total_now" => null,
                "total_before" => "20,422" // arr[0] + arr[1] + arr[2] ..... = 20000
            ]
        ];
        $totalNow = 0;
        $totalBefore = 0;
        $array1 = [];
        foreach ($array as $arr) {
            if ($arr['total_now'] != null) {
                $arr['total_now'] = $totalNow += (float) preg_replace('/\D/ui', '', $arr['total_now']);
            }
            if ($arr['total_before'] != null) {
                $arr['total_before'] = $totalBefore += (float) preg_replace('/\D/ui', '', $arr['total_before']);
            }
            $array1[] = $arr;
        }

我假设您正在处理整数值并且您的逗号代表数千个占位符。这些需要进行消毒才能进行添加。

我已经包括了 null 检查,它假定一旦在当年遇到空日期,就不会有后续的数值。

代码:(Demo)

public function GetMonthlyTotalsSincePreviousYear($year = null): void
{
    $thisYear = $year ?? date('Y');
    $lastYear = $thisYear - 1;
    $thisYearRunningTotal = 0;
    $lastYearRunningTotal = 0;
    $result = [];
    for ($month = 1; $month <= 12; ++$month) {
        $thisYearMonthTotal = $this->My_model->model_month($month, $thisYear)->row()->hasil;
        $thisYearRunningTotal = $thisYearMonthTotal === null
            ? null
            : (int)str_replace(',', '', $thisYearMonthTotal) + $thisYearRunningTotal;
        
        $lastYearMonthTotal = $this->My_model->model_month($month, $lastYear)->row()->hasil;
        $lastYearRunningTotal = $lastYearMonthTotal === null
            ? null
            : (int)str_replace(',', '', $lastYearMonthTotal) + ($lastYearRunningTotal ?? 0);

        $result[] = [
            'month' => $month,
            'total_now' => $thisYearRunningTotal,
            'total_before' => $lastYearRunningTotal,
        ];
    }
    $this->set_response($result, 200);  
}