访问 SimpleXMLElement 中的数字属性

发布于 2024-09-02 11:34:33 字数 1536 浏览 5 评论 0原文

我正在尝试访问以下元素中的数字,但无法从中获取值。

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 技术交流群。

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

发布评论

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

评论(4

稀香 2024-09-09 11:34:34

XML 元素不能以数字开头。即使您可以以某种方式创建此类元素,SimpleXML(很可能是大多数解析器)也将无法读取结果文档。

<!-- legal -->
<a0>foo</a0>

<!-- not legal -- note how even Stack Overflow's highlighter chokes on it -->
<0>foo</0>

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.

<!-- legal -->
<a0>foo</a0>

<!-- not legal -- note how even Stack Overflow's highlighter chokes on it -->
<0>foo</0>
漫漫岁月 2024-09-09 11:34:33
$x = 0;
echo $object->$x;

或者

echo $object->{0};

原因是“0”在 PHP 中不是有效的标识符。因此,当您输入“0”时,它看到的只是 T_LNUMBER。所有名称都遵循变量命名约定。唯一的偏差是成员变量前面带有 -> 。不需要 $ 前缀。 http://www.php.net/manual/en/language.variables .basics.php

{0} 有效,因为 {} 表示标识符是内部简单表达式的结果。因此,在本例中,{$x} 与 $x 相同,但 {0} 与“0”不同,因为它们会产生不同的解析器标记。

$x = 0;
echo $object->$x;

or

echo $object->{0};

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.

青春有你 2024-09-09 11:34:33

我不知道名为“0”的节点有什么关系,但您看到的错误是因为 SimpleXML 始终返回对象。如果必须将结果用作数字,请将其转换为适当的类型,例如

number_format((int) $ruleXml->HourlyUsage)

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.

number_format((int) $ruleXml->HourlyUsage)
放飞的风筝 2024-09-09 11:34:33

您可以使用 __toString() 来获取子对象的字符串值,所以它会像 $object->__toString();

You can use __toString() to get string value of child object ,so it will be like $object->__toString();

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