数组转换成xml数据
<?php $arr=array( 'username'=>'huahua', 'password'=>'123456', 'number'=>'15889652911', ); echo arrayToXml($arr); function arrayToXml($arr){ $xml = "<root>"; foreach ($arr as $key=>$val){ if(is_array($val)){ $xml.="<".$key.">".arrayToXml($val)."</".$key.">"; }else{ $xml.="<".$key.">".$val."</".$key.">"; } } $xml.="</root>"; //返回xml数据 header('Content-type:text/xml;charset=utf-8'); return $xml; }
xml格式数据转换成数组
<?php $xml="<root><username>huahua</username><password>123456</password><number>15889652911</number></root>"; var_dump(xmlToArray($xml)); function xmlToArray($xml){ //禁止引用外部xml实体 libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring),true); return $val; }