".$key.">";" Just convert the array into xml."/> ".$key.">";" Just convert the array into xml.">
Home > Article > Backend Development > How to convert php array to xml
How to convert php array to xml: first create a PHP sample file; then create an arrayToXml method; finally pass "$xml.="d34df75372f4a53fbb2bbc0f061a0b1b97124ee9a9ee721ef98319cc2ceab842a0cd70d8ea9baf105402a6df3ab5b85c";" Just convert the array into xml.

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.
Convert php array to XML format, and convert XML format to array
//数组转XML
function arrayToXml($arr)
{
$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转为array
function xmlToArray($xml)
{
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}[Recommended learning: PHP video tutorial]
The above is the detailed content of How to convert php array to xml. For more information, please follow other related articles on the PHP Chinese website!