简体   繁体   中英

make json array in json codeigniter

please help, i got stucked on making json using codeigniter.

this is what i want my json look like

在此处输入图像描述

{
  "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"
        }
      ]
    }
  ]
}

but, i dont know why my code doesnt work to make like it.

this is how my code output: 在此处输入图像描述

{
  "pelajar": {
    "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 my code:

$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);

my problem is to set 'pendidikan' in the pelajar list where id_pelajar from pendidikan is same with id from pelajar table.

Honestly, there is so much to fix, this script should probably be completely rewritten, but I am not prepared to do that from my phone.

I would recommend using Active Record techniques in your model (not your controller).

Cache the subarray data before pushing into the first level. In doing so, you maintain the relationship between the parent id and the subarray data.

To be clear, my snippet will always create a pendidikan subarray -- even if it has no data. If you do not want this behavior, you will need to modify the script to check if the subarray is empty and then conditionally include it into the parent array. If this was my project, I would prefer a consistent data structure so that subsequent process wouldn't need to check again if specific keys exist.

Untested code:

$query = $this->db->query("SELECT * FROM learn")->result();
$data = [];
foreach($query as $row){
    $datap = [];
    $pendidikanquery = $this->db->query("SELECT * FROM pendidikan WHERE learn_id = {$row->id}")->result();
    foreach ($pendidikanquery as $pen) {
        $datap[] = [
            "id_pelajar" => $pen->id_pelajar,
            "sekolah" => $pen->sekolah
        ];
    }
    $data[] = [
        "id" => $row->id,
        "name" => $row->name,
        "usia" => $row->usia,
        "tahun" => $row->tahun,
        "alamat" => $row->alamat,
        "pendidikan" => $datap
    ];
}

header('Content-Type: application/json');
echo json_encode(["pelajar" => $data]);

if you want the output like your first image(the one with a red square)-

$response['pelajar']['pendidikan'] = $datap; // change this one to
// this ↓↓
$response['pelajar']['pelajar']['pendidikan'] = $datap; // change it like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM