PHP获取值从数组的SimpleXMLElement [英] PHP get values from SimpleXMLElement array
本文介绍了PHP获取值从数组的SimpleXMLElement的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这样的:
[1] =>
对象(的SimpleXMLElement)#6(1){
[@属性] =>
阵列(14){
[名称] =>
串(5)的MySQL
[承认] =>
串(1)为1
[意见] =>
串(1)为1
[current_check_attempt] =>
串(1)为1
[停机] =>
串(1)为0
[LAST_CHECK] =>
串(19)2010-05-01 17时57分00秒
[markdown_filter] =>
串(1)为0
[max_check_attempts] =>
串(1)3
[输出] =>
串(42)严重 - 套接字超时10秒后
[perfdata_available] =>
串(1)为1
[service_object_id] =>
串(3)580
[状态] =>
字符串(8)关键
[state_duration] =>
串(6)759439
[未处理] =>
串(1)为0
}
}
(我用的var_dump($子)来生成)
我如何得到'名'出有属性作为一个字符串?
下面是我的code:
$ XML = simplexml_load_string($结果);的foreach($ XML的>数据 - >名单$子){
后续代码var_dump($子女);
回声$儿童为>的getName()。 :。儿童期$>的名字。 < BR />中;
}
解决方案
通过SimpleXML的,你可以得到:
- 子元素,使用对象表示法:
$元素 - >子元素
- 和属性,使用数组表示法:
$元素['属性']
结果
所以,在这里,我说你不得不使用:
回声$子['名'];
结果
作为参考,并为一对夫妇的例子,请参见 基本用法 simplexml的手册的部分。
示例#6 应该是有趣的,关于属性。
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 />";
}
解决方案
With SimpleXML, you can get :
- sub-elements, using object notation :
$element->subElement
- and attributes, using array notation :
$element['attribute']
So, here, I'd say you'd have to use :
echo $child['name'];
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.
这篇关于PHP获取值从数组的SimpleXMLElement的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文