访问 SimpleXMLElement 中的数字属性
我正在尝试访问以下元素中的数字,但无法从中获取值。
echo $object->0; //returns Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '$'
SimpleXMLElement Object (
[0:public] => 15810
)
关于如何获得该价值有什么想法吗?
更新
我意识到这是一个奇怪的错误...我正在使用 eBay API 来获取此值。即使我这样做了:
$zero = 0;
$print_r($ruleXml->HourlyUsage->$zero);
它仍然显示
SimpleXMLElement Object (
[0:public] => 15810
)
我尝试过的 {0}
以及
这是我正在处理的输出......
[1] => SimpleXMLElement Object (
[CallName:public] => AddItem
[CountsTowardAggregate:public] => false
[DailyHardLimit:public] => 100000
[DailySoftLimit:public] => 100000
[DailyUsage:public] => 0
[HourlyHardLimit:public] => 100000
[HourlySoftLimit:public] => 100000
[HourlyUsage:public] => 0
[Period:public] => -1
[PeriodicHardLimit:public] => 0
[PeriodicSoftLimit:public] => 0
[PeriodicUsage:public] => 0
[ModTime:public] => 2010-05-04T18:06:08.000Z
[RuleCurrentStatus:public] => NotSet
[RuleStatus:public] => RuleOn
)
所以这就是事情......
number_format($ruleXml->HourlyUsage) //throws the error: number_format() expects parameter 1 to be double, object given
$ruleXml->HourlyUsage //shows the value on the page
I'm trying to access the number in the below element, but I'm having trouble getting the value out of it.
echo $object->0; //returns Parse error: syntax error, unexpected T_LNUMBER, expecting T_STRING or T_VARIABLE or '{' or '
Any ideas on how I can get that value?
Update
I realize that this is an odd error... I'm using the ebay API to get this value. Even when I do:
$zero = 0;
$print_r($ruleXml->HourlyUsage->$zero);
It still shows the same
SimpleXMLElement Object (
[0:public] => 15810
)
I tried {0}
as well
Here's the output of what I'm working with....
[1] => SimpleXMLElement Object (
[CallName:public] => AddItem
[CountsTowardAggregate:public] => false
[DailyHardLimit:public] => 100000
[DailySoftLimit:public] => 100000
[DailyUsage:public] => 0
[HourlyHardLimit:public] => 100000
[HourlySoftLimit:public] => 100000
[HourlyUsage:public] => 0
[Period:public] => -1
[PeriodicHardLimit:public] => 0
[PeriodicSoftLimit:public] => 0
[PeriodicUsage:public] => 0
[ModTime:public] => 2010-05-04T18:06:08.000Z
[RuleCurrentStatus:public] => NotSet
[RuleStatus:public] => RuleOn
)
So here's the thing...
number_format($ruleXml->HourlyUsage) //throws the error: number_format() expects parameter 1 to be double, object given
$ruleXml->HourlyUsage //shows the value on the page
SimpleXMLElement Object (
[0:public] => 15810
)
Any ideas on how I can get that value?
Update
I realize that this is an odd error... I'm using the ebay API to get this value. Even when I do:
It still shows the same
I tried {0}
as well
Here's the output of what I'm working with....
So here's the thing...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
XML 元素不能以数字开头。即使您可以以某种方式创建此类元素,SimpleXML(很可能是大多数解析器)也将无法读取结果文档。
XML elements cannot begin with a digit. Even if you can somehow create such elements, SimpleXML (and most likely, most parsers) will not be able to read the result document.
或者
原因是“0”在 PHP 中不是有效的标识符。因此,当您输入“0”时,它看到的只是 T_LNUMBER。所有名称都遵循变量命名约定。唯一的偏差是成员变量前面带有 -> 。不需要 $ 前缀。 http://www.php.net/manual/en/language.variables .basics.php
{0} 有效,因为 {} 表示标识符是内部简单表达式的结果。因此,在本例中,{$x} 与 $x 相同,但 {0} 与“0”不同,因为它们会产生不同的解析器标记。
or
The reason is that '0' is not a valid identifier in PHP. So when you type '0', all it sees is a T_LNUMBER. All names follow the varaible naming convention. The only deviation is that a member variable preceded by a -> does not need the $ prefix. http://www.php.net/manual/en/language.variables.basics.php
{0} works, because {} indicates that the identifier is the result of the simple expression inside. So {$x} is the same as $x in this case, but {0} is not the same as '0', since they result in different parser tokens.
我不知道名为“0”的节点有什么关系,但您看到的错误是因为 SimpleXML 始终返回对象。如果必须将结果用作数字,请将其转换为适当的类型,例如
I don't know what that business about nodes named "0" but the error you're seeing is because SimpleXML always returns objects. If you have to use the result as a number, cast it to the appropriate type, e.g.
您可以使用 __toString() 来获取子对象的字符串值,所以它会像
$object->__toString();
You can use
__toString()
to get string value of child object ,so it will be like$object->__toString();