phparrayscodeigniterforeacharray-push

How to push an array into json key/value pair of an object i codeigniter array


What am I trying to achieve

So what I was trying to achieve was push an array key/value pair into a JSON object that is in itself an array, so that there is a single object and one of the key-value pairs contains an array.

THIS IS WHAT I AM TRYING TO ACHIEVE as JSON output, another data2 Array inside another data array.

   [
    {
    "data": {
        "temp_service_id": "3",
        "name": "health checkup",
        "price": "10000",
        "service_id": "41",
        "data2": [
             {
              "fees": "2000",
              "service_name": "Anaesthesiologist"
             },
             {
              "fees": "300",
              "service_name": "Andrologist"
             },
           ]
        },  
     }
   ] 

What I have tried
THIS IS MY CONTROLLER :

        $where['subtype_id'] = $this->post('org_id');
        $where['is_active'] = 'Y';
        $table_package_master = 'package_master';
        $fetch_package_name = $this->$model_name->fetch($table_package_master,$where);
        $array = [];
        $array1 = [];
        $array2 = [];
        if($fetch_package_name){
            foreach($fetch_package_name as $row){
                $where_r['t1.package_num'] = $row->package_num;
                $where_r['t1.is_active'] = 'Y';
                $where_r['services.is_active'] = 'Y';
                $where_r['t4.is_active'] = 'Y';
                $fetch_packages1 = $this->$model_name->fetch_packages1($where_r);
                $array['data'] = $fetch_packages1;
                $fetch_packages = $this->$model_name->fetch_packages($where_r);
                foreach($fetch_packages as $row1){
                    $where_re['services.service_id'] = $row1->service_id;
                    $where_re['services.is_active'] = 'Y';
                    $where_re['template_services.is_active'] = 'Y';                          

                    $fetch_package_ser = $this->$model_name->fetch_service_details($where_re);
                    array_push($array1,$fetch_package_ser);
                }
            }
            $array['data2'] = $array1;
            $array3 = [$array];
            $this->response($array3);
          }

THIS IS MY MODEL :

             function fetch($table,$where_con){
             $this->db->select('*')->from($table)->where($where_con);
              return $this->db->get()->result();
                }

                function fetch_packages1($where){
             $this->db->select('t3.temp_service_id,t4.name,t4.price,services.service_id');
              $this->db->from('package_services as t1');
             $this->db->join('services','services.service_id = t1.service_id', 'LEFT');
             $this->db->join('template_services as t3','t3.temp_service_id = services.temp_service_id' , 'LEFT');
             $this->db->join('package_master as t4', 't4.package_num = t1.package_num','LEFT');
             $this->db->where($where);
             $this->db->group_by('t1.package_num');
             return $this->db->get()->row();
           }

          function fetch_service_details($where){
           $this->db->select('services.price as fees,template_services.service_name');
           $this->db->from('services');
           $this->db->join('template_services','template_services.temp_service_id = 
           services.temp_service_id','LEFT');
           $this->db->where($where);
           return $this->db->get()->row();
            }

Basically what I am trying to do is put a data object inside $array variable that is data coming from one table now what i did was push data from another query into $array1 Than at the end all i did was array_push into $array3 all the data from $array1 and $array2 to make them join together but MY OUTPUT IS THIS :

         [
            {
             "data": {
                "temp_service_id": "3",
                "name": "health checkup",
                "price": "10000",
                "service_id": "41"
                     },
             "data2":  [
                     {
               "fees": "2000",
               "service_name": "Anaesthesiologist"
                     },
                     {
               "fees": "300",
               "service_name": "Andrologist"
                     }
               ]
            }
        ]

What am i missing here? i am really confused and can't understand how to push data2 array. Thankyou in advance :)


Solution

  • I came up with a solution finally after thinking about it a lot

         $fetch_package_name = $this->$model_name->fetch($table_package_master,$where);
            $array = [];
            $array1 = [];
            if($fetch_package_name){
                for($i=0;$i<sizeof($fetch_package_name); $i++){
                    $where_r['t1.package_num'] = $fetch_package_name[$i]->package_num;
                    $where_r['t1.is_active'] = 'Y';
                    $where_r['services.is_active'] = 'Y';
                    $where_r['t4.is_active'] = 'Y';
                    $fetch_packages1 = $this->$model_name->fetch_packages1($where_r);
    
                    $array[$i]['name'] = $fetch_package_name[$i]->name;
                    $array[$i]['price'] = $fetch_package_name[$i]->price;
                    $where_re['t1.package_num'] = $fetch_package_name[$i]->package_num;
                    $where_re['t1.is_active'] = 'Y';
                    $where_re['services.is_active'] = 'Y';
                    $fetch_packages = $this->$model_name->fetch_packages2($where_re);
                    for($j=0; $j<sizeof($fetch_packages); $j++){
                        $array1[$j]['service_name'] = $fetch_packages[$j]->service_name;
                        $array1[$j]['price'] = $fetch_packages[$j]->price;
                    }
                    $array[$i]['data2'] = $array1;
                }
                // $this->response($array);
                $array2['data'] = $array;
                $array2['status'] = 10007; 
                // $data_response['data'] = $array;
                $this->response($array2);
        }       
    

    IT gave me the response that i desired in the first place.\

      { 
         "data":[ 
               { 
                "name":"Full body checkup",
                 "price":"5000",
                    "data2":[ 
                        { 
                        "service_name":"Allergist",
                        "price":"23"
                        },
                        { 
                        "service_name":"Andrologist",
                        "price":"300"
                        }
                     ]
                } 
          ],
      }
    

    Cheers! happy coding :-)