Skip to content
Advertisement

Merge multiple CodeIgniter result sets into one array

Data queried from the database.

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

and

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

Here I create a method called `GetData(), I create a loop from 1-12 based on the months in a year. Next I want to combine data in an array, but how do I combine the data?

Below is my controller model which calls the same model method over and over with different parameters:

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

I want an output like this:

[
    {
        "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"
    },
]

In 2021, the total is only until the month of 02, while in 2020 the total is until month 12.

If 2021 is only up to month 02 then the next array is total null.

Advertisement

Answer

This, I believe, will assist you in developing the solution. I haven’t included the entire array of $year_2020, but I hope you get the idea –

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

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}]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement