【问题标题】:PHP read object attributesPHP读取对象属性
【发布时间】:2018-03-07 04:16:07
【问题描述】:

我有一个像这样的对象名称obj

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)

【问题讨论】:

    标签: php attributes stdclass


    【解决方案1】:

    您需要将@attributes 包装在{} 中 检查示例

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-22
      • 2021-02-26
      相关资源
      最近更新 更多