Home > Article > Backend Development > PHP array generates Xml format
This article mainly introduces the Xml format generated by PHP arrays, which has certain reference value. Now I share it with everyone. Friends in need can refer to it
Methods to generate Xml when the public account is developed
public 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>"; return $xml; }
xml converted to array
function xmlToArray($xml,$isfile=false){ //禁止引用外部xml实体 libxml_disable_entity_loader(true); if($isfile){ if(!file_exists($xml)) return false; $xmlstr = file_get_contents($xml); }else{ $xmlstr = $xml; } $result= json_decode(json_encode(simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA)), true); return $result; }
Related recommendations:
Code to generate CSV file from a PHP array
The above is the detailed content of PHP array generates Xml format. For more information, please follow other related articles on the PHP Chinese website!