代码点火器MySql中的Json数组问题

时间:2015-12-05 09:44:24

标签: mysql json codeigniter

以下是我的代码:

    $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"}}

我不明白怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个

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

输出

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