SimpleXMLElement - 获取节点的属性


SimpleXMLElement - get an attribute of a node

我有一个SimpleXMLObject $node,其中包含一个我必须检索的数组"data",但我不知道如何检索。 我有$node['data'],但它不起作用。 有什么帮助吗?

也许这很简单,但我不知道如何快速完成。

编辑:示例代码

foreach ($xmlObject->children() as $node) {
    if($node->getName() == 'URL_web_images'){
        // here i have a $node that contains an array named 'URL_web_image', how to access to it?
    }
}

SimpleXML 是一个类,$node似乎是它的实例。您不能在 php 中访问数组等属性。你必须使用$obj->property.

所以试试这个:

echo $node->data;

试试这个

print_r(json_decode($node));
$node_json = json_decode($node);
echo $node_json['data'];