In this paper, we describe the method of Array2xml class in PHP to transform the array into XML. Share to everyone for your reference. The implementation method is as follows:
<?phpclass 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, just take the space before the text is the key list ($key,) =explode (", $key); } $xml. = "< $key >"; $xml. =is_array ($val)? $this->_array2xml ($val): $val; Remove the space, just take the space before the text is the key list ($key,) =explode (", $key); $xml. = "</$key >"; } return $xml; }}
I hope this article is helpful to everyone's PHP programming.