加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP从SimpleXMLElement数组中获取值

发布时间:2020-12-13 21:28:17 所属栏目:PHP教程 来源:网络整理
导读:我有这个: [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" ["las
我有这个:

[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 />";
  }

解决方法

使用SimpleXML,您可以获得:

>子元素,使用对象表示法:$element-> subElement
>和属性,使用数组表示法:$element [‘attribute’]

所以,在这里,我要说你必须使用:

echo $child['name'];

作为参考,有关几个示例,请参阅simplexml手册的Basic usage部分.

示例#6应该是有趣的,关于属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读