Home  >  Article  >  Backend Development  >  Function to convert PHP array to XML

Function to convert PHP array to XML

不言
不言Original
2018-04-20 11:29:061539browse

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 == &#39;&#39;) return &#39;&#39;;
libxml_disable_entity_loader(true);
$arr = json_decode(json_encode(simplexml_load_string($xml, &#39;SimpleXMLElement&#39;, 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn