加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Codeigniter,如何获取以json格式显示的数据库表数据?

发布时间:2020-12-13 21:56:09 所属栏目:PHP教程 来源:网络整理
导读:我必须以json格式显示结果. 我的代码看起来像: $this-db-select("*"); $this-db-from('country'); $query = $this-db-get(); foreach($query-result() as $row) { echo json_encode($row); } 结果如下: {"id":"1","country_name":"Australia","country_cod
我必须以json格式显示结果.
我的代码看起来像:

$this->db->select("*");
        $this->db->from('country');  
        $query = $this->db->get();

        foreach($query->result() as $row)
        {
            echo json_encode($row);
        }

结果如下:

{"id":"1","country_name":"Australia","country_code":"61"}{"id":"2","country_name":"Bangladesh","country_code":"880"}{"id":"3","country_name":"Brazil","country_code":"55"}

我必须在[]中得到这个json并用逗号(,)分隔.这是我想要的结果如下:

[{"id":"1","country_code":"61"},{"id":"2","country_code":"880"},{"id":"3","country_code":"55"}]

我应该在代码中完成所有更改.

解决方法

检索数组中的记录,然后对其进行编码

$result = $this->db->get('country')->result_array();
print_r(json_encode($result));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读