【问题标题】:Json Array Issue in Code-igniter MySqlCode-igniter MySql 中的 Json 数组问题
【发布时间】:2016-03-10 06:38:00
【问题描述】:

下面是我的代码:

    $this->db->select("city_id,loc_id,loc_name");
    $this->db->from("locations");
    $this->db->group_by("city_id");
    $this->db->order_by("city_id", "asc");
    $query = $this->db->get();
    return json_encode($query->result_array());

它给我的结果如下:

[{"city_id":"1","loc_id":"17","loc_name":"Al Barsha"} {"city_id":"2","loc_id":"12","loc_name":"testt"}]

但我想得到喜欢

{"1":{"17":"Al Barsg","35":"XYZ"},"2":{"123":"ABC","44":"OtherName"}}

我不明白怎么做?

【问题讨论】:

  • 你应该为它创建字符串!
  • 如果有城市ID,如何创建字符串。我需要用城市表创建连接查询吗?
  • 如果你不想 city_id 从你的查询中删除它

标签: mysql json codeigniter


【解决方案1】:

试试这个

return json_encode(array($query->result_array());

输出

["0":{"city_id":"1","loc_id":"17","loc_name":"Al Barsha"}...

【讨论】:

  • 感谢您的回复,但我只需要 loc_id 和 loc_name 像 ["0":{"17":"Al Barsha"......} , "1":{"18" :"another"} 其中 0 和 1 是 city_ids
  • 它不可行。循环通过这个。 Cz 如果你想要元素的 id,你无法得到它。用这种方式
  • 所以你的意思是,我想要的不可能?
  • 假设您想删除一条记录。这样你就可以使用 id。但在你的情况下,它无法完成。
猜你喜欢
  • 2012-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-02
  • 2014-09-16
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
相关资源
最近更新 更多