简体   繁体   English

访问数组中的对象属性时出错

[英]Error accessing an object property in an array

I am trying to access value of 'user' in the array below. 我正在尝试访问下面数组中的“用户”值。 I can't seem to get anything to show. 我似乎什么也没显示。 This is my code: 这是我的代码:

$data = json_encode($values);

When I var_dump the above code, I get the below: 当我var_dump上面的代码时,我得到以下内容:

[
  {
    "user": "xxxx",
    "category": "xxxx",
    "email": "joe@gmail.com"
  }
]

Now I want to get value of 'user' and I did this: 现在我想获得“用户”的价值,我做到了:

echo $data[0]->user;

But this does not show anything. 但这什么也没显示。 Please what am I doing wrong? 请问我做错了什么?

As your using json_encode($values) to give you $data , $data is just a string - the JSON you show in the question, and $data[0]->user doesn't work on a string. 当您使用json_encode($values)给您$data$data只是一个字符串-您在问题中显示的JSON,而$data[0]->user对字符串不起作用。

If you want the value of user , you should use your original data, I'm assuming $values ... 如果您想要user的值,则应该使用原始数据,我假设$values ...

echo $values[0]->user;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM