Accessing stdClass variable name that start's with an integer value [duplicate]
Possible Duplicate:
How do I access this object property?
The data returned in a request is (JSON):
stdClass Object
(
[USD] => stdClass Object
(
[7d] => 23.3414
I'm calling:
json_decode(...);
And trying to access it via:
echo $json->USD->7d;
But that fails, because the variable name cannot start with an integer. Is there any syntax in PHP开发者_开发知识库 for accessing this?
Otherwise I would fix this by doing:
$set = (array) $json->USD;
echo $set['7d'];
This is working:
$name = '7bar';
$o->$name = 'foo';
echo $o->{'7bar'};
As you see in the example there are two different ways to access variables with uncommon names.
json_decode($var, true);
sets object to an assoc array, not your answer but saves typecast call.
Why not change the variable name from 7d to something like d7? Is there a reason for the 7d reference?
精彩评论