简体   繁体   中英

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:

[
  {
    "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.

If you want the value of user , you should use your original data, I'm assuming $values ...

echo $values[0]->user;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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