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

发布于 2024-09-18 06:56:40 字数 275 浏览 1 评论 0原文

我试图从 XML 中提取一些数据,但是当我执行以下命令时,我得到了

警告:为 ... 中的 foreach() 提供的参数无效

代码示例:

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

它对于单个单词的节点名称工作正常,所以我猜测它不喜欢连字符。我需要逃避他们吗?如果需要的话如何逃避?

I am trying to extract some data from XML but when I execute the following I get a

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

Code Example:

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

It works fine for node names that are single words so I am guessing that it doesn't like the hyphens. Do I need to escape them and if so how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚我心虐我身 2024-09-25 06:56:45

来自 PHP 手册

访问 XML 文档中包含 PHP 命名约定不允许的字符(例如连字符)的元素可以通过将元素名称封装在大括号和撇号内来完成。

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

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

From 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.

In your case you do:

$xml->{'custom-field-value'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文