将多个CodeIgniter结果集合并为一个数组



从数据库查询的数据。

year 2021 = [
{
"month": 1,
"total": "1,482"
},
{
"month": 2,
"total": "5,422"
},
]

year 2020 = [
{
"month": 1,
"total": "2,482"
},
{
"month": 2,
"total": "6,422"
},
{
"month": 3,
"total": "7,422"
},
.........
{
"month": 12,
"total": "20,422"
},
]

在这里我创建了一个名为"GetData()"的方法,我根据一年中的月份创建了一个从1到12的循环。接下来我要组合数组中的数据,但如何组合数据呢?

下面是我的控制器模型,它使用不同的参数反复调用相同的模型方法:

public function GetData(){
$thn_now = 2021; 
$thn_before = 2020; 
$bulan = array();
for ($bul = 1; $bul <= 12; $bul++) {
$sql = $this->My_model->model_month($bul,$thn_now)->result();  // Here I make the current year parameter
// $sql2 = $this->My_model->model_month($bul,$thn_before)->result();  //here are the parameters of the previous year
foreach($sql as $row) {
$bulan[] = array(
'bulan' => $bul,
'total_now' => $row->hasil,
// how to display the previous year's total in an array ?
// 'total_before' => $row->hasil,
);
}
}
$this->set_response($bulan,200);  
}

我想要这样的输出:

[
{
"month": 1,
"total_now": "1,482"
"total_before": "2,482"
},
{
"month": 2,
"total_now": "5,522"
"total_before": "6,422"
},
{
"month": 3,
"total_now": null 
"total_before": "7,422"
},
.........
{
"month": 12,
"total_now": null,
"total_before": "20,422"
},
]

在2021年,总数只到02月,而在2020年,总数到12月。

如果2021只到第02个月,那么下一个数组是总null

  1. 我不建议您访问数据库24次以收集个人总数;我将敦促您创建一个新的模型方法,在一次旅行中提供所有的月度总数。
  2. 我不建议允许您的控制器指定从模型中的活动记录创建的结果集的类型。在整个应用程序中,模型应该始终以相同的结构返回相同的数据。
  3. 在我看来,你的内部循环无论如何只收集一行,所以不是创建对象数组的result(),而是调用创建null或对象的row(),然后立即访问hasil属性。使用null合并操作符,当hasil不是非对象null的属性时,您可以退回到null
  4. 世界各地的专业程序员(包括不以英语为第一语言的程序员)都会推荐使用英语单词编写变量。这使得其他开发人员可以使用更一致的语言来审查和维护您的代码。毕竟,php语法的其余部分都是用英语写的,所以还是用英语吧。
  5. 为了将来的灵活性,允许"本年";作为控制器方法参数传入。这将允许您无缝地查看历史数据(如果需要的话)。如果没有传入参数,则默认为当前年份。
  6. 硬编码每个月的数字是可以接受的,因为它永远不会改变。然而,作为一般规则,尽量避免在脚本中使用硬编码的数字/值,这样它更灵活,更容易维护,更直观地阅读。

建议代码(允许您将多达24个活动记录对象传递回控制器):

public function GetMonthlyTotalsSincePreviousYear($year = null): void
{
$thisYear = $year ?? date('Y');
$lastYear = $thisYear - 1;
$result = [];
for ($month = 1; $month <= 12; ++$month) {
$result[] = [
'month' => $month,
'total_now' => $this->My_model->model_month($month, $thisYear)->row()->hasil ?? null,
'total_before' => $this->My_model->model_month($month, $lastYear)->row()->hasil ?? null,
];
}
$this->set_response($result, 200);  
}

在我自己的应用程序中,我将有一个收集所有数据的模型方法和一个像这样的控制器:

public function getMonthlyTotalsSincePreviousYear(int $year = null): void
{
$this->set_response(
$this->My_model->getMonthlyTotalsSincePreviousYear($year ?? date('Y')),
200
);
}

我相信这将有助于您开发解决方案。我没有包括$year_2020的整个数组,但我希望您能理解-

$year_2021 = [
[
"month" => 1,
"total_now" => "1,482"
],
[
"month" => 2,
"total_now" => "5,422"
]
];
$year_2020 = [
[
"month" => 1,
"total_before" => "2,482"
],
[
"month" => 2,
"total_before" => "6,422"
],
[
"month" => 3,
"total_before" => "7,422"
]
];
$output = [];
foreach ($year_2021 as $obj) {
$key = $obj['month'];
$output[$key] = $obj;
}
foreach ($year_2020 as $obj) {
$key = $obj['month'];
if (isset($output[$key])) {
$output[$key]['total_before'] = $obj['total_before'];
} else {
$obj['total_now'] = null;
$output[$key] = $obj;
}
}
$output = array_values($output);
error_log(json_encode($output));

输出
[{"month":1,"total_now":"1,482","total_before":"2,482"},{"month":2,"total_now":"5,422","total_before":"6,422"},{"month":3,"total_before":"7,422","total_now":null}]

最新更新