【发布时间】: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"
},
]
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