This article describes how to convert arrays into XML in the Array2xml class of php. The example shows how to convert arrays into XML and Array2xml is a very practical array conversion technique, for more information about how to convert arrays into XML, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:
The 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. =" ";
}
Return $ xml;
}
}
I hope this article will help you with PHP programming.