数组转换成xml数据
<?php
$arr=array(
'username'=>'name',
'password'=>'123456',
'number'=>'13266668888',
);
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>name</username><password>123456</password><number>13266668888</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;
}
网友评论