Home >Backend Development >PHP Tutorial >PHP method to convert array into xml
The example in this article describes the method of converting array into xml in PHP. Share it with everyone for your reference, the details are as follows:
<?php $elementLevel = 0 ; function array_Xml($array, $keys = '') { global $elementLevel; if(!is_array($array)) { if($keys == ''){ return $array; }else{ return "\n<$keys>" . $array . "</$keys>\n"; } }else{ foreach ($array as $key => $value) { $haveTag = true; if (is_numeric($key)) { $key = $keys; $haveTag = false; } if($elementLevel == 0 ) { $startElement = "<$key>"; $endElement = "</$key>"; } $text .= $startElement; if(!$haveTag) { $elementLevel++; $text .= "<$key>" . array_Xml($value, $key). "</$key>\n"; }else{ $elementLevel++; $text .= array_Xml($value, $key); } $text .= $endElement; } } return $text; } $array = array( "employees" => array( "employee" => array( array( "name" => "name one", "position" => "position one" ), array( "name" => "name two", "position" => "position two" ), array( "name" => "name three", "position" => "position three" ) ) ) ); echo array_Xml($array); ?>
I hope this article will be helpful to everyone in PHP programming.
For more articles related to PHP's methods of converting arrays into xml, please pay attention to the PHP Chinese website!