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.