PHP 从 SimpleXMLElement 数组获取值
我有这个:(
[1]=>
object(SimpleXMLElement)#6 (1) {
["@attributes"]=>
array(14) {
["name"]=>
string(5) "MySQL"
["acknowledged"]=>
string(1) "1"
["comments"]=>
string(1) "1"
["current_check_attempt"]=>
string(1) "1"
["downtime"]=>
string(1) "0"
["last_check"]=>
string(19) "2010-05-01 17:57:00"
["markdown_filter"]=>
string(1) "0"
["max_check_attempts"]=>
string(1) "3"
["output"]=>
string(42) "CRITICAL - Socket timeout after 10 seconds"
["perfdata_available"]=>
string(1) "1"
["service_object_id"]=>
string(3) "580"
["state"]=>
string(8) "critical"
["state_duration"]=>
string(6) "759439"
["unhandled"]=>
string(1) "0"
}
}
我使用 var_dump($child) 来生成它)
如何以字符串形式获取“name”属性?
这是我的代码:
$xml = simplexml_load_string($results);
foreach($xml->data->list as $child) {
var_dump($child);
echo $child->getName() . ": " . $child->name . "<br />";
}
I have this:
[1]=>
object(SimpleXMLElement)#6 (1) {
["@attributes"]=>
array(14) {
["name"]=>
string(5) "MySQL"
["acknowledged"]=>
string(1) "1"
["comments"]=>
string(1) "1"
["current_check_attempt"]=>
string(1) "1"
["downtime"]=>
string(1) "0"
["last_check"]=>
string(19) "2010-05-01 17:57:00"
["markdown_filter"]=>
string(1) "0"
["max_check_attempts"]=>
string(1) "3"
["output"]=>
string(42) "CRITICAL - Socket timeout after 10 seconds"
["perfdata_available"]=>
string(1) "1"
["service_object_id"]=>
string(3) "580"
["state"]=>
string(8) "critical"
["state_duration"]=>
string(6) "759439"
["unhandled"]=>
string(1) "0"
}
}
(I used var_dump($child) to generate that)
How do I get the 'name' attribute out of there as a string?
Here is my code:
$xml = simplexml_load_string($results);
foreach($xml->data->list as $child) {
var_dump($child);
echo $child->getName() . ": " . $child->name . "<br />";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用 SimpleXML,您可以获得:
$element->subElement
$element['attribute']
< br>
所以,在这里,我想说你必须使用:
作为参考和一些示例,请参阅 Basic simplexml 手册的用法部分。
示例#6应该是有趣的,关于属性。
With SimpleXML, you can get :
$element->subElement
$element['attribute']
So, here, I'd say you'd have to use :
As a reference, and for a couple of examples, see the Basic usage section of simplexml's manual.
Example #6 should be the interesting one, about attributes.
虽然您可以执行以下操作:
查看该值,但您应该注意
$child['name']
是一个对象,而不是字符串。回显它会将其转换为字符串,因此它在这种情况下可以工作。但如果您将其存储在某个地方,最好自己将其转换为字符串:While you can do:
to see the value, you should note that
$child['name']
is an object, not a string. Echoing it casts it to a string, so it works in that situation. But if you're storing it somewhere, it's better to cast it to a string yourself:有点混乱,但我成功地使用了它
不知道为什么,但 OpsView API 返回一个二维数组,而不是每个 XML 节点只有一个值:(
有效并且更加优雅,谢谢。
Kind of messy, but I used this successfully
Not sure why, but the OpsView API returns a two-dimensional array instead of just having one value per XML node :(
works and is much more elegant, thank you.
当我第一次遇到这个问题时,这真的很令人困惑。在尝试获取整个数组时,我似乎返回了数组中的第一个对象!
在我的实例中,“item”是一个对象数组,以下仅返回数组中的第一个!
但是,如果像我一样,您想要访问所有对象,您只需循环项目即可,所以这并不是什么问题。真的很奇怪!
This was really confusing when i first encountered this. While trying to get the whole array, instead I was seemingly returned the first object in the array instead!
In my instance "item" was an array of objects and the following only returned the first in the array!
However if like me, you want to access all the objects you can just loop over item, so not really a problem. Just really strange!
我遇到了类似的问题,我需要从 SimpleXMLElement 中获取字符串,但找不到调用它的名称。找到了解决方案,通过使用 (string) 获取字符串文本:
I had a similar problem, I needed to get the string out of my SimpleXMLElement, I couldn't find the name to call it. Found the solution, by using (string) to get the string text: