【问题标题】:Unable to access the array object that contains @ sign无法访问包含@符号的数组对象
【发布时间】:2012-07-05 09:47:32
【问题描述】:

我有一个在对象中包含 @sign 的数组。我无法访问它
实际上我正在将 XML 解析为数组
print_r 输出如下

[link] => SimpleXMLElement Object
                        (
                            [@attributes] => Array
                                (
                                    [type] => text/html
                                    [href] => http://www.livingsocial.com/cities/1964-klang-valley-kuala-lumpur/deals/393950-2-pax-steamboat-set-2-iced-lemon-tea
                                    [rel] => alternate
                                )

                        )



$entry->link->@attributes->href
无法访问

【问题讨论】:

  • 试试 $entry->link->{'@attributes'}
  • 语法不正确,xml元素的属性不能有@符号

标签: php web


【解决方案1】:

您必须使用attributes() 方法从 SimpleXMLElement 对象中获取属性

 $entry->link->attributes();

如果您想将 href 属性作为字符串获取,您可以执行以下操作:

$href = $entry->link->attributes()->href->__toString();

【讨论】:

    猜你喜欢
    • 2012-10-09
    • 2019-06-13
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-14
    • 1970-01-01
    相关资源
    最近更新 更多