代码之家  ›  专栏  ›  技术社区  ›  nanya stack

在php中如何将数据对象推送到对象数组

  •  0
  • nanya stack  · 技术社区  · 2 年前

    我有嵌套的数据数组 在hire中,如何向数组['data']推送新的值? 我拥有的数据是一个对象,我想将数据插入每个数组

    [
            {   
                "divisi" : 01,
                "data": [
                    {
                        "status": "MUNDUR",
                        "total": "0"
                    }
                ]
            },
            {   
                "divisi" : 02,
                "data": [
                    {
                        "status": "BATAL",
                        "total": "0"
                    }
                ]
            },
            {...},
            {...},
        ]
    

    后果

    {   
      "divisi" : 01,
      "data": [
                {
                 "status": "BATAL",
                 "total": "0"
                },
                {
                 "status": "WIN", // new push
                 "total": "0"
                }
              ]
    },
    

    这是我的密码

    public function detail_get(){
            $divisi = $this->M_model->db_tbl_divisi();
            $data =array();
            foreach ($divisi as $row){
                $i = $this->M_model->db_realisasi_divisi($row['divisi_kode']);
                $win = $this->M_model->db_win_win($row['divisi_kode'],'win'); // how to push ?
                $data[] = array(
                    'divisi' => $row['kode'], // 01,02
                    'data' => $i
                );
            }
            $response =  $this->set_response($data,200);
        }
    

    我有密码

    $win = $this->M_model->db_win_win($row['divisi_kode'],'win'); 
    
    value object 
    {
     "status": "win",
     "total": "1"
    }
    

    我有一个数据对象,我想插入到['data'] 怎么做

    1 回复  |  直到 2 年前
        1
  •  0
  •   Justinas    2 年前

    我猜 $i 是数组的数组,也是数组的数组 array_push($i, $win) (或 $i[] = $win )

        public function detail_get() {
            $divisi = $this->M_model->db_tbl_divisi();
            $data = [];
            foreach ($divisi as $row){
                $i = $this->M_model->db_realisasi_divisi($row['divisi_kode']);
                $i[] = $this->M_model->db_win_win($row['divisi_kode'], 'win');
    
                $data[] = [
                    'divisi' => $row['kode'], // 01,02
                    'data' => $i,
                ];
            }
    
            $response = $this->set_response($data,200);
        }