2017-09-26 258 views
3

我有一個對象名稱obj這樣的:PHP讀取對象屬性

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

我使用的一些方法:

  • obj[CurrencyCode]

  • obj ->CurrencyCode

但不工作,得到錯誤:Use of undefined constant CurrencyCode 如果使用: obj -> @attributes得到錯誤:syntax error, unexpected ''@attributes'' (T_CONSTANT_ENCAPSED_STRING)

回答

3

你需要用在{}@attributes檢查例子

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