带有@符号的PHP stdobject用于密钥[复制]

This question already has an answer here:

From a json decode, I have an std that has a key called

@MyKey

Note the @ symbol prefix. I'm not sure how to access it because doing:

$item['@MyKey']

Results in

 Cannot use object of type stdClass as array

And doing

$item->@MyKey

Results in

 syntax error, unexpected '@', expecting T_STRING or T_VARIABLE
</div>

Just figured it out:

$item->{'@MyKey'}

You can access them using the curly syntax. Example:

$json = '{ "@test"  : { "@foo" : "bar" }}';

$obj = json_decode($json);

echo $obj->{'@test'}->{'@foo'};

you can cast the item to an array and use array accessors as normal

$itemArray = (array)$item; 

echo $itemArray["@MyKey"];