In php, The Array2xml class converts arrays into XML instances. array2xmlxml
This article describes how to convert arrays into XML in the Array2xml class of php. Share it with you for your reference. The specific implementation method is as follows:
Copy codeThe Code is as follows:
<? Php
Class Array2xml
{
Var $ xml;
Function array2xml ($ array, $ encoding = 'utf-8 '){
$ This-> xml = '<? Xml version = "1.0" encoding = "'. $ encoding.'"?> ';
$ This-> xml. = $ this-> _ array2xml ($ array );
}
Function getXml (){
Return $ this-> xml;
}
Function _ array2xml ($ array)
{
$ Xml = '';
Foreach ($ array as $ key => $ val ){
If (is_numeric ($ key )){
$ Key = "item id = \" $ key \"";
} Else {
// Remove the space. Only the text before the space is used as the key
List ($ key,) = explode ('', $ key );
}
$ Xml. = "<$ key> ";
$ Xml. = is_array ($ val )? $ This-> _ array2xml ($ val): $ val;
// Remove the space. Only the text before the space is used as the key
List ($ key,) = explode ('', $ key );
$ Xml. = "</$ key> ";
}
Return $ xml;
}
}
I hope this article will help you with PHP programming.