I'm receiving a JSON and then decoding it either to object or associative array, then I need to loop through an specific item (results->playerStatus->name).
Here's the structure:
{
"results": [
{
"id": 1,
"name": "DEMO",
"description": "Description",
"platform": "PLAYER",
"lastContactInMinutes": null,
"group": {
"id": 1,
"name": "Group DEMO"
},
"playerStatus": {
"id": 6,
"name": "Never accessed",
"time": 0
},
"playlists": {
"0": {
"id": 1,
"name": "Playlist DEMO"
},
"1": {
"id": 1,
"name": "Playlist DEMO"
},
"2": {
"id": 1,
"name": "Playlist DEMO"
},
"3": {
"id": 1,
"name": "Playlist DEMO"
},
"4": {
"id": 1,
"name": "Playlist DEMO"
},
"5": {
"id": 1,
"name": "Playlist DEMO"
},
"6": {
"id": 1,
"name": "Playlist DEMO"
}
},
"audios": {
"0": null
}
}]
}
Here's what I tried
$object = json_decode($feed);
foreach ($object->results->playerStatus->name as $p) {
echo $p;
}
I got an error 'cause, obviously, I don't know the correct syntax.
Parse error: syntax error, unexpected ''results'' (T_CONSTANT_ENCAPSED_STRING)
What is the correct syntax to achieve this?
The array is $object->results
, you need to do the looping at that level, then access the contents using object notation.
foreach ($object->results as $result) {
$name = $result->playerStatus->name;
echo $name;
}