Home > Article > Backend Development > How to convert php array to xml
php array to xml method: first create a PHP sample file; then define an "arrayToXml" method; then use "is_numeric" and other functions to convert array to xml; and finally output the converted xml.
Recommended: "PHP Video Tutorial"
Code:
/** * 数组转xml字符 * @param string $xml xml字符串 **/ function arrayToXml($data){ if(!is_array($data) || count($data) <= 0){ return false; } $xml = "<xml>"; foreach ($data as $key=>$val){ if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; }
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!