数组转xml格式/xml格式转数组
数组转xml格式
$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>";
return $xml;
}
xml格式转数组
$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;
}