thinkphp Source Learning data_to_xml function data into XML format

Source: Internet
Author: User

/**
* 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.