在PHP中使用带连字符的XML节点名称 [英] Using XML node names with hyphens in PHP

查看:41
本文介绍了在PHP中使用带连字符的XML节点名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

代码示例:

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?

推荐答案

来自PHP 手册:

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

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'}

这篇关于在PHP中使用带连字符的XML节点名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆