How to convert php array to xml: first create a PHP sample file; then create an arrayToXml method; finally pass "$xml.="<".$key.">< in the method body ![CDATA[".$val."]]>".$key.">";" 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!