1348831860 1234567890123456 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)); ruby 24 company
结果:
2017/10/15 更新互转的方法以及转数组的另外一种方法:下面代码来自 http://www.cnblogs.com/txw1958/p/php-xml-to-array.html
//数组转XML function arrayToXml($arr) { $xml = ""; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val." "; }else{ $xml.="<".$key."> "; } } $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; }