Reputation: 794
I am getting a JSON response which looks like this:
stdClass Object
(
[location00] => Array
(
[0] => stdClass Object
(
[id_0] => Array
(
[0] => stdClass Object
(
[id] => 1
[name] => Wanted by Aryurumoka
[gold_reward] => 58900
[event] => 0
[description] => Not provided.
)
)
)
)
)
For example, i am able to get [name]
by $quests->location00[0]->id_0[0]->name
.
Lets say i create a new variable $location = 'location00'
. Now if i try $quests->$location[0]->id_0[0]->name'
, i am getting Undefined property: stdClass::$l
error. I tried $location = 'location00[0]'
as well however i have completly no idea why this happenes. How can i assign location00
to variable to use it while parsing JSON?
Upvotes: 1
Views: 48
Reputation: 1793
You can use associative array or $obj->{$var} :
<?php
$quests = json_decode($json, true);
$location = 'location00';
$name = $quests[$location][0]['id_0'][0]['name'];
Upvotes: 2
Reputation: 191749
I'd try to get new JSON, but you can interpolate object property retrieval with braces:
$quests->{$location}[0]
Upvotes: 1