Home >Backend Development >PHP Tutorial >Detailed explanation of conversion examples between PHP XML and arrays
This article mainly introduces relevant information on the detailed explanation of the mutual conversion between PHP XML and arrays. Friends in need can refer to the following
PHP XML and array mutual conversion
//数组转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; }
Five predefined entities in the XML file:
d0339ea0abeb4f1275ddb6c994f9f846 | ##>greater than number | |
& | and | |
' | apostrophe | |
" | Double quotes |
|
Related recommendations:
##PHP Method to implement asynchronous execution of script
PHP method to implement regular replacement of punctuation marks with spaces
The above is the detailed content of Detailed explanation of conversion examples between PHP XML and arrays. For more information, please follow other related articles on the PHP Chinese website!