“在标志处” @ 在 SimpleXML 对象中?
这是在典型的 SimpleXMLElement 对象上运行 print_r()
的输出:
SimpleXMLElement Object
(
[@attributes] => Array
(
)
)
@ 符号是什么意思?
This is the output of print_r()
run on a typical SimpleXMLElement object:
SimpleXMLElement Object
(
[@attributes] => Array
(
)
)
What does the @ sign mean?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是一个 SimpleXMLElement 对象。 “@attributes”行是 属性 的内部表示XML 元素。使用 SimpleXML 的函数从该对象获取数据,而不是直接与其交互。
This is a SimpleXMLElement object. The '@attributes' row is an internal representation of the attributes from the XML element. Use SimpleXML's functions to get data from this object rather than interacting with it directly.
所有关于错误控制的答案都是不正确的。 @ 并不意味着任何。这就是该属性在内部的调用方式,但不要依赖于此。处理 SimpleXML 时不要依赖
print_r()
或var_dump()
。 SimpleXML 做了很多print_r()
和var_dump()
无法正确表示的“神奇”事情。如果您需要了解 XML 片段“内部”的内容,只需使用
->asXML()
即可。All those answers about error control are incorrect. The @ doesn't mean anything. That's how the property is called internally, but do not rely on this. Do not rely on
print_r()
orvar_dump()
when dealing with SimpleXML. SimpleXML does a lot of "magical" things that are not correctly represented byprint_r()
andvar_dump()
.If you need to know what's "inside" a XML fragment, just use
->asXML()
on it.抱歉,无法以访客身份发表评论,但对于像我一样最终来到这里的其他人...我正在创建自己的 Joomla 表单字段,而 Joomla 创建了一个非常“有趣”的各种事物对象。现在,我不想成为 SimpleXML 专家,我想要的只是隐藏在 @attributes 中的原始标签文本。
经过一段时间的“嗯,我想知道这是否有效?”™ 我发现这是访问这些值的最简单方法:
他们没有说谎。这确实很简单。
Sorry, can't comment as a guest but for anyone else who ends up here like I did... I am creating my own Joomla form fields and Joomla creates a very 'interesting' object of all sorts of things. Now, I didn't want to become a SimpleXML expert, all I wanted was the original label text which was squirrelled away in @attributes.
After a bit of
"hmmm, I wonder if this works?"™
I found this is the easiest way of accessing these values:They were not lying. It really is simple.
我正在使用仅提供 XML 格式数据的 HTTP API。所以首先我将它加载到 SimpleXML 中,并且还对 @attributes 问题感到困惑..我如何获取它包含的宝贵数据? print_r() 让我困惑。
我的解决方案是在 0 处创建一个数组和一个迭代器变量。使用 foreach 循环遍历 SimpleXML 对象,并使用 attribues() 方法获取数据并将其加载到我创建的数组。在 foreach 循环结束之前进行迭代。
所以 print_r() 从显示这个:
到一个更可用的普通数组。这很棒,因为我希望能够在需要时快速将数组转换为 json。
我的代码解决方案:
快速说明 PHP 的 settype() 函数很奇怪/有缺陷,因此我添加了 + 以确保 ID 是整数,并添加了 + >引号以确保名称是字符串。如果没有某种类型的变量转换,您将把 SimpleXML 对象加载到您创建的数组中。
print_r()的最终结果:
I am working with an HTTP API that gives out only XML formatted data. So first I loaded it into SimpleXML and was also puzzled by the @attributes issue.. how do I get at the precious data it contains? print_r() confused me.
My solution was to create an array and an iterator variable at 0. Loop through a SimpleXML object with foreach and get at the data with the attribues() method and load it into my created array. Iterate before foreach loop ends.
So print_r() went from showing this:
To a much more usable normal array. Which is great because I wanted the option to quickly convert array into json if needed.
My solution in code:
Quick note would be PHP's settype() function is weird/buggy, so I added the + to make sure ID is an integer and added the quotes to make sure the name is string. If there isn't a variable conversion of some kind, you're going to be loading SimpleXML objects into the array you created.
Final result of print_r():