简体   繁体   中英

PHP read object attributes

I have an Object name obj like that:

stdClass Object
(
    [@attributes] => stdClass Object
        (
            [CurrencyCode] => AUD
            [CurrencyName] => AUST.DOLLAR
            [Buy] => 17825.4
            [Transfer] => 17933
            [Sell] => 18092.95
        )
)

I use some ways:

  • obj[CurrencyCode]

  • obj ->CurrencyCode

but don't work and get error: Use of undefined constant CurrencyCode If use: obj -> @attributes to get error: syntax error, unexpected ''@attributes'' (T_CONSTANT_ENCAPSED_STRING)

You need to wrap the @attributes in the {} Check the example

<?php
 $abc = array('@attributes' => array('CURR' => 1));
$abc = json_decode(json_encode($abc));
echo '<pre>';
print_r($abc->{"@attributes"}->CURR);
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM