用php创建json数组

时间:2017-12-04 06:23:27

标签: php arrays json codeigniter-3

我发现很难创建JSON数据。 我想要制作JSON,如下图所示

enter image description here

但我遇到了麻烦。 我只能制作如下图所示

enter image description here

我创建了以下代码。请帮帮我,我真的很困惑

foreach ($q->result() as $row){
  $djson[] = array(
    'id' => $row->id_custome,
    'name' => $row->nama_custome
    );
}
return $djson;

1 个答案:

答案 0 :(得分:3)

您应该使用键/索引

        foreach ($q->result() as $key=>$row){
          $djson[ ( $key + 1 ) ] = array(
              'id' => $row->id_custome,
              'name' => $row->nama_custome
          );
        }
        return $djson;

注意:$key + 1因为你想从1启动json。