我正在尝试从 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/