Search code examples
phpattributesstdclass

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)


Solution

  • 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);
    ?>