在json codeigniter中制作json数组

请帮助,我被卡在使用codeigniter制作json上了。

这就是我想要我的json看起来像

enter image description here

{
  "pelajar": [
    {
      "id": "1",
      "name": "rumah sakit",
      "usia": "45",
      "tahun": "2020",
      "alamat": "jalan kartini"
    },
    {
      "id": "2",
      "name": "rumah sakit umum",
      "usia": "28",
      "tahun": "2020",
      "alamat": "jalan ibu",
      "pendidikan": [
        {
          "id_pelajar": "2",
          "sekolah": "SDN Lombok timur"
        },
        {
          "id_pelajar": "2",
          "sekolah": "SMPN Lombok timur"
        }
      ]
    }
  ]
}

但是,我不知道为什么我的代码无法正常工作。

this is how my code output : enter image description here

这是我的代码:

$query = $this->db->query("select * from learn") -> result();
        $response = array();
        $data = array();
        $datap = array();

        foreach($query as $row){

            $data[] = array(
                    "id"=> $row->id,
                    "name"=>$row->name,
                    "usia"=>$row->usia,
                    "tahun"=>$row->tahun,
                    "alamat"=>$row->alamat

            );

            $id = $row->id;
            $pendidikanquery = $this->db->query("select * from pendidikan where learn_id='$id'" ) -> result();
            foreach($pendidikanquery as $pen){
                $datap[] = array(
                    "id_pelajar"=> $pen->id_pelajar,
                    "sekolah"=> $pen->sekolah
                );
            }


            }
        }

        $response['pelajar']['pelajar'] = $data;
        $response['pelajar']['pendidikan'] = $datap;

        header('Content-Type: application/json');
        echo json_encode($response, TRUE);

我的问题是在pelajar列表中设置“ pendidikan”,其中来自pendidikan的id_pelajar与来自pelajar表的id相同。

评论