数组参数与xml数据包互转

浏览:954 时间:2019-09-04 16:41:51

/*数组参数xml数据包*/
public function ArrToXml($arr){
$xml ="<xml>";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= "<".$key.">".$val."";
}else{
$xml .= "<".$key."><![CDATA[".$val."]]>";
}
}
$xml .= "</xml>";
return $xml;
}
/*xml转为数组*/
public function XmlToArr($xml){
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}


标签: xml 数组