JSON using PHP to get selected values
535
Been looking thru stackoverflow about this so I did try to get this correct. Not sure what I'm doing wrong here. Any suggestions?
<?php
$string = '{
"status": "success",
"th_size": "small",
"users": [
{
"user_id": 159826,
"username": "WillBeUsed",
"online": true,
"is_away": false,
"access": "public",
"render_info": {
"profile_url": "/person1",
"profile_image": "11e5a496f13366a0c4ce000403aa862",
"language": "english",
"chat": "ready"
},
"new": false,
"back": false
}
],
"online_count": 1
}';
$result = json_decode($string, true);
//echo $result['status']; // This works
echo $result->users[0]->render_info->profile_url; // This don't work
?>
Answer
Solution:
You didn't look hard enough. The PHP manual clearly states the parameters for
json_decode
:By passing the second parameter as
true
you are telling it to give you back an associative array (not an object), so you need to access it like this: