简体   繁体   English

在 json codeigniter 中制作 json 数组

[英]make json array in json codeigniter

please help, i got stucked on making json using codeigniter.请帮忙,我被困在使用 codeigniter 制作 json 上。

this is what i want my json look like这就是我想要我的 json 的样子

在此处输入图像描述

{
  "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:这就是我的代码 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.我的问题是在 pelajar 列表中设置“pendidikan”,其中来自 pendidikan 的 id_pelajar 与来自 pelajar 表的 id 相同。

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).我建议在您的 model(不是您的控制器)中使用 Active Record 技术。

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.这样做,您维护了父 id 和子数组数据之间的关系。

To be clear, my snippet will always create a pendidikan subarray -- even if it has no data.需要明确的是,我的代码片段总是会创建一个pendidikan子数组——即使它没有数据。 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)-如果您想要 output 像您的第一张图片(带有红色方块的图片)-

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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