在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
<?php $xml = <<<XML <xml> <ToUserName><![CDATA[toUser]]></ToUserName> <FromUserName><![CDATA[fromUser]]></FromUserName> <CreateTime>1348831860</CreateTime> <MsgType><![CDATA[text]]></MsgType> <Content><![CDATA[this is a test]]></Content> <MsgId>1234567890123456</MsgId> <Person> <Name>ruby</Name> <Age>24</Age> <Company> <Name>company</Name> </Company> </Person> </xml> XML; /** * @param SimpleXMLElement $xmls * @return array */ function parseXml($xmls) { $array = []; foreach ($xmls as $key => $xml) { /** @var SimpleXMLElement $xml */ $count = $xml->count(); if ($count == 0) { $res = (string) $xml; } else { $res = parseXml($xml); } $array[$key] = $res; } return $array; } $xmls = new SimpleXMLElement($xml); var_dump(parseXml($xmls)); 结果:
2017/10/15 更新互转的方法以及转数组的另外一种方法:下面代码来自 http://www.cnblogs.com/txw1958/p/php-xml-to-array.html //数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } //将XML转为array function xmlToArray($xml) { //禁止引用外部xml实体 libxml_disable_entity_loader(true); $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $values; }
|
2022-07-18
2022-08-16
2022-11-06
2022-08-18
2022-08-15
请发表评论