Php array to xml and xml conversion array _ PHP Tutorial

Source: Internet
Author: User
Tags php xml to array
Php array to xml and xml conversion array instances. This article introduces two simple examples: converting php arrays to xml and xml arrays. I hope this article will help you. Php array to xml code: Copy the code below. This article will introduce two simple examples. php array to xml and xml to convert arrays. I hope this article will help you.

Php array to xml

The code is as follows:

Function array2xml ($ array, $ xml = false ){
If ($ xml = false ){
$ Xml = new SimpleXMLElement (' ');
}
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 );

Php xml to array

The code is as follows:

$ S = <

Basic
1
4
3
1
2


EOS;

$ A = json_decode (json_encode (array) simplexml_load_string ($ s), 1 );
Print_r ($ );

Convert the arrays into xml and xml conversion arrays. I hope this article will help you. Php array to xml code is as follows...

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.