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

xggvc2p6  于 2022-12-07  发布在  PHP
关注(0)|答案(1)|浏览(141)

我有嵌套在hire中的数据数组,如何将新值推送到array ['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']如何操作

yc0p9oo0

yc0p9oo01#

我假设$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);
    }

相关问题