【问题标题】:Accumulate monthly totals while looping循环时累积每月总计
【发布时间】:2021-10-11 00:59:06
【问题描述】:

我有一组数据,其中包含当年和上一年的月度金额。上一年总是有它的所有金额,但是当当前年份的当前/未来月份被迭代时,它有一个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"
    },
]

这是my current code

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,
        ];
    }
}

如何将上个月的金额转入当月以保持每年的逐月运行总额?此外,一旦一个月返回 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
    },
]

【问题讨论】:

  • 你能分享一下你从上面的代码中看到的预期输出吗?
  • 我已附上支票,兄弟
  • 查看答案
  • 我不是已经回答过这个问题了吗?你放弃这个问题了吗?哦,我明白了,你接受了另一个答案,但显然在这个问题上使用了我的建议。看起来很公平。
  • 我不明白你的回答@mickmackusa

标签: php arrays codeigniter sum


【解决方案1】:

看来你希望它像----

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

【讨论】:

  • 为什么在只包含 \D 的模式上还有 ui 模式修饰符?
  • 我试过了,但是循环时,2021 年 9-12 月的总数应该为空,你能在我的代码中举个例子吗?在那里,我用 row 调用了两个不同年份的模型
  • 如果使用 foreach ,我应该循环哪个模型?
  • 这些松散的比较会将零值误认为是空值。
【解决方案2】:

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

我已经包含了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);  
}

【讨论】:

    猜你喜欢
    • 2019-04-24
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2021-01-04
    • 2013-12-02
    相关资源
    最近更新 更多