PHP json从数据库解码 [英] PHP json decode from database

查看:44
本文介绍了PHP json从数据库解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中有这个 json编码,我只想在 foreach 中回显值 name_r ,这是怎么回事?

I have this json encode in the database, and i want just echo values name_r in foreach , how is it?

[{
    "name_r": "saeed",
    "units": ["salam", "11", "11", "khobe", "22", "22", "salam"],
    "price_change": ["33", "33", "33", "44", "44", "44"]
}, {
    "name_r": "salavat",
    "units": ["55", "55", "khobe", "66", "66"],
    "price_change": ["77", "77", "77", "88", "88", "88", "99", "99", "99"]
}]

这是我的php,发生错误(<消息>消息:未定义的索引:name_r-行号:179 ):

this my php, that have error(Message: Undefined index: name_r - Line Number: 179):

$query = $this->db->query("SELECT * FROM table ORDER BY id desc");
    $data = array();
    foreach ($query->result() as $row)
    {
        $data[] = json_decode($row->residence,true);
        echo $data["name_r"].'<br>'; //Line Number: 179
    }

推荐答案

假设您提供的 json_encoded 数据存储在数据库的一行中,则 json_decode 将给你一个数组数组.要回显所有 name_r 字段,您需要:

Assuming that the json_encoded data you provided is stored in one row of the database, the json_decode will give you an array of arrays. To echo all the name_r fields, you would need to:

foreach ($query->result() as $row){
    $data = json_decode($row->residence,true);
    foreach($data as $datum){
        echo $datum['name_r'];
    }
}

这篇关于PHP json从数据库解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆