Home >Backend Development >PHP Tutorial >Function to convert PHP array to XML
This article mainly introduces the functions for converting PHP arrays and XML. It has certain reference value. Now I share it with everyone. Friends in need can refer to it
//数组转xml function ArrToXml($arr) { if(!is_array($arr) || count($arr) == 0) return ''; $xml = "<xml>"; foreach ($arr as $key=>$val) { if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } //Xml转数组 function XmlToArr($xml) { if($xml == '') return ''; libxml_disable_entity_loader(true); $arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $arr; }
Related recommendations:
php array function sequence array_pop() deletes the last element in the array
The above is the detailed content of Function to convert PHP array to XML. For more information, please follow other related articles on the PHP Chinese website!