First look at an array to convert XML
The code is as follows |
Copy Code |
function Array2xml ($array, $xml = False) { if ($xml = = False) { $xml = new SimpleXMLElement (' <root/> '); } foreach ($array as $key => $value) { if (Is_array ($value)) { Array2xml ($value, $xml->addchild ($key)); }else{ $xml->addchild ($key, $value); } } return $xml->asxml (); }
Header (' Content-type:text/xml '); Print Array2xml ($array); |
So if there is no Chinese characters, no problem.
If there is a problem with Chinese, when the content appears to be empty when the Chinese character, the solution is the transcoding process
The code is as follows |
Copy Code |
/* Convert PHP array into XML */
function Array2xml ($array, $xml = False) { if ($xml = = False) { $xml = new SimpleXMLElement (' <root/> '); } foreach ($array as $key => $value) { if (Is_array ($value)) { Array2xml ($value, $xml->addchild ($key)); }else{ $value =utf8_encode ($value);
if (Preg_match ("/([X81-xfe][x40-xfe])/", $value, $match)) { $value = Iconv (' GBK ', ' utf-8 ', $value); Determine if any Chinese characters appear } $xml->addchild ($key, $value); } } return $xml->asxml (); } |