Skip to content
Advertisement

merge object in php

i have below json data

{
    "data": [
        {
            "minggu a": "2",
            "total a": "377033"
        },
        {
            "minggu b": "1",
            "total b": "136615"
        }
    ]

and this is the code i made in controller

$satu= $this->model->db_week_resTender();
     $arr = [];
     foreach($satu as $val){
        $arr['data'][] = array(
            'minggu' => $val['minggu'],
            'total'  => $val['total']
        );
     }
     $response =  $this->set_response($arr,200);
}

how to concatenate below json data here I issue data with a limit of 2.

  {
        "data": [
            {
                "minggu a": "2",
                "total a": "377033"
                "minggu b": "1",
                "total b": "136615"
            },
        ]
}

Advertisement

Answer

$data = '{
       "data": [
                {
                  "minggu a": "2",
                  "total a": "377033"
                },{
                  "minggu b": "1",
                  "total b": "136615"
                  }
                ]
          }';

$decoded_satu=json_decode($data,true);
$arr = [];
foreach($decoded_satu['data'] as $key=> $val){
        foreach ($val as $subkey => $subvalue) {
                $arr['data'][$subkey] = $subvalue;
        }
}

$finalArray = json_encode($arr);
echo "<pre>";print_r($finalArray) ;die;
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement