Recursive implementation of PHP array to XML code sharing

Source: Internet
Author: User

This article takes the example form to narrate the PHP realization array recursive escape method, shares for everybody to use for the reference. The specific methods are as follows:

PHP to convert the array of XML requirements are common, and there are many ways to implement, Baidu looked for a variety of implementation methods, but the basic is to borrow a group of some components. I wrote it myself. A method of string spellings that supports multidimensional arrays. For reference only, the lack of information please do not hesitate to enlighten!

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21-22 /** * Converts an array to an XML * @param array $data the arrays to be converted * @param bool $root whether to root node * @return String XML string * @author Dragondean * @url Http://www.cnblogs.com/dragondean/function Arr2xml ($data, $root = True) {$str = ""; if ($root) $str. = "<xml>"; fore Ach ($data as $key => $val) {if (Is_array ($val)) {$child = Arr2xml ($val, False) $str. = "< $key > $child </$key > "; } else{$str. = "< $key ><![ cdata[$val]]></$key > "; } if ($root) $str. = "</xml>"; return $str; }

The above is the implementation of the method, the first parameter is the array you want to convert, the second optional parameter set whether the need to add root node, default is required.

Test code:

Copy code code as follows:

$arr =array (' A ' => ' aaa ', ' B ' =>array (' C ' => ' 1234 ', ' d ' => "ASDFASDF"));

echo Arr2xml ($arr);

The result of code execution is:

Copy code code as follows:

  

The above mentioned is the entire content of this article, I hope you can enjoy.

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.