无法从数组中获取值

时间:2012-04-25 17:58:25

标签: php json

我正在尝试输出数组的电子邮件值的值,但是这样做有问题。 该数组基于json_decode()

这是我收到的错误

Fatal error: Cannot use object of type stdClass as array in /home/.... line 57

JSON(值:$ this-> bck_content)

{"email":"test@email.com","membership_id":"0","fname":"Kenneth","lname":"Poulsen","userlevel":"1","created":"2012-04-23 10:57:45","lastlogin":"2012-04-23 10:58:52","active":"y"}

我的代码

    # Display requested user details
    $details_array = json_decode($this->bck_content);

    $value = $details_array['email'];
    print $value;

2 个答案:

答案 0 :(得分:3)

您需要使用json_decode的第二个参数来强制JS对象上的数组结构。

json_decode($this->bck_content, true);

这将确保json中的所有JS对象都被解码为关联数组而不是PHP StdObjects。

当然,假设您要使用数组表示法来访问它们。如果您使用对象表示法没问题,那么您可以使用:

$value = $details_array->email;

答案 1 :(得分:1)

尝试这个

$value = $details_array->email;

json_decode($json, true);

$details_array = (array)json_decode($json);

你做错了什么是在错误描述中写的

相关问题