【发布时间】:2017-01-24 11:14:21
【问题描述】:
打印出来的 JSON 类似于:
{
"c_id": {
"value": 66
},
"first_name": {
"value": "Joe"
},
...
这来自下面的代码
$profile = json_encode($this->session->getProfile(), true);
echo $profile;
echo $profile['first_name']['value'];
但是,我得到了错误:
Illegal string offset 'first_name'
Illegal string offset 'value'
有什么想法吗?我看不出我做错了什么。
谢谢
【问题讨论】:
-
$profile不再是array而是string -
json_encode 返回一个字符串
-
如果这个
$this->session->getProfile()应该返回一个JSON字符串,使用json_decode($this->session->getProfile())