/**
* Data XML encoding
* @param mixed $data data
* Node name when @param string $item a numeric index
* @param string $id Numeric index key to the property name
* @return String
*/
Function data_to_xml ($data, $item = ' item ', $id = ' id ') { $xml = $attr = '; foreach ($data as $key => $val) { if (Is_numeric ($key)) { $id && $attr = " {$id}=\" {$key}\ ""; // {$id}=\ "{$key}\" after execution id= "1" where \ is an escape character, escapes the double quote character $key = $item; } $xml .= "<{$ key}{$attr}> ";//$key for node name $ATTR for properties $xml .= (is_array ($val) | | is_object ($val)) ? data_to_xml ($val, $item, $id) : $val; $xml .= "</{$key}>"; } return $xml;} $data = array ( ' note ' =>array ( ' from ' + ' liming ', ' to ' = ' Xiaowang ', ' message ' = ' hello,world! ' ), $a =data_to_xml ($data); Var_dump ($a);
The output result is
String ' <note> <from>liming</from> <to>xiaowang</to> <message>hel Lo,world!</message> </note> ' (length=80)
Note: The result format has been changed manually!
This article is from the "11400485" blog, please be sure to keep this source http://11410485.blog.51cto.com/11400485/1833518
thinkphp Source Learning data_to_xml function data into XML format