Home > Backend Development > PHP Tutorial > Code sharing for recursively converting php array to xml_PHP tutorial

Code sharing for recursively converting php array to xml_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 09:53:53
Original
822 people have browsed it

Code sharing for recursively converting php arrays to xml

This article describes the method of recursively escaping arrays in PHP in the form of examples and shares it with you for your reference. The specific method is as follows:

The need to convert arrays to xml in PHP is common, and there are many implementation methods. Baidu looked for various implementation methods, but basically they borrowed some components. I wrote a string grouping method myself, which supports multi-dimensional arrays. This is for reference only, please feel free to let us know if there are any deficiencies!

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

/**

* 将数组转换为xml

* @param array $data 要转换的数组

* @param bool $root 是否要根节点

* @return string xml字符串

* @author Dragondean

* @url http://www.cnblogs.com/dragondean

*/

function arr2xml($data, $root = true){

$str="";

if($root)$str .= "";

foreach($data as $key => $val){

if(is_array($val)){

$child = arr2xml($val, false);

$str .= "<$key>$child";

}else{

$str.= "<$key>";

}

}

if($root)$str .= "";

return $str;

}

1

2

3

4

5

6

7

8

9

10 11 12

13

14 15

1617 18 19 20 21 22
/**
* Convert array to xml * @param array $data Array to be converted * @param bool $root whether to want the root node * @return string xml string * @author Dragondean * @url http://www.cnblogs.com/dragondean */ function arr2xml($data, $root = true){ $str=""; if($root)$str .= ""; foreach($data as $key => $val){ if(is_array($val)){ $child = arr2xml($val, false); $str .= "<$key>$child"; }else{ $str.= "<$key>"; } } if($root)$str .= ""; return $str; }
The above is the implementation method. The first parameter is the array you want to convert. The second optional parameter sets whether to add a root node. It is required by default. Test code: Copy the code. The code is as follows:  $arr=array('a'=>'aaa','b'=>array('c'=>'1234' , 'd' => "asdfasdf")); echo arr2xml($arr); The result after executing the code is: Copy the code. The code is as follows:   The above is the entire content of this article, I hope you all like it. http://www.bkjia.com/PHPjc/1000078.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1000078.htmlTechArticleCode sharing for recursively converting php arrays to xml. This article describes the method of recursively escaping arrays in PHP in the form of examples. Share it with everyone for your reference. The specific method is as follows: PHP Lieutenant General...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template