开发者

Accessing stdClass variable name that start's with an integer value [duplicate]

This question already has answers here: Closed 10 years ago.

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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜