In php, the Array2xml class converts arrays into XML instances.

Source: Internet
Author: User
This article describes how to convert arrays into XML in the Array2xml class of php. The example shows how to convert arrays into XML and Array2xml is a very practical array conversion technique, for more information about how to convert arrays into XML, see the example in this article. Share it with you for your reference. The specific implementation method is as follows:

The code is as follows:


<? Php
Class 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. only the text before the space is used as the key
List ($ key,) = explode ('', $ key );
}
$ Xml. = "<$ key> ";
$ Xml. = is_array ($ val )? $ This-> _ array2xml ($ val): $ val;
// Remove the space. only the text before the space is used as the key
List ($ key,) = explode ('', $ key );
$ Xml. =" ";
}
Return $ xml;
}
}

I hope this article will help you with PHP programming.

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.