simplexml - 在 PHP 中使用带有连字符的 XML 节点名称

标签 simplexml php

我正在尝试从 XML 中提取一些数据,但是当我执行以下操作时,我得到了一个

Warning: Invalid argument supplied for foreach() in ...

代码示例:

foreach ($xml->custom-field-value as $milestone)
{
    ...     
}

它适用于单个单词的节点名称,所以我猜它不喜欢连字符。我需要逃避它们吗?如果需要,怎么办?

最佳答案

来自 PHP manual :

Accessing elements within an XML document that contain characters not permitted under PHP's naming convention (e.g. the hyphen) can be accomplished by encapsulating the element name within braces and the apostrophe.

在你的情况下,你这样做:

$xml->{'custom-field-value'}

关于simplexml - 在 PHP 中使用带有连字符的 XML 节点名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3634599/

相关文章:

php - 在多维数组的子数组中查找公共(public)值

php - Twitter Bootstrap HTML 表单单选按钮不适用于 PHP POST

php - 使用 PHP 脚本从站点中删除未使用的 CSS

php - preg_replace 正则表达式模式

php - simplexml 并访问 feedburner 的 :

php - DOMDocument::schemaValidate() 抛出警告错误

php - 比较 SimpleXml 对象

php - 什么时候使用 real_escape_string 比较合适?数据何时以 POST 方式到达,或者就在编写查询之前到达?

php - for 循环内对当前 SimpleXMLElement 对象的 Xpath 查询

PHP: preg_match() 不正确