Home  >  Article  >  Backend Development  >  Recursive implementation of converting php array to xml

Recursive implementation of converting php array to xml

WBOY
WBOYOriginal
2016-08-08 09:24:17918browse

The need to convert PHP winning arrays into xml 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. For reference only, please feel free to let us know if there are any deficiencies!

<span>/*</span><span>*
*   将数组转换为xml
*    @param array $data    要转换的数组
*   @param bool $root     是否要根节点
*   @return string         xml字符串
*    @author Dragondean
*    @url    http://www.cnblogs.com/dragondean
</span><span>*/</span><span>function</span> arr2xml(<span>$data</span>, <span>$root</span> = <span>true</span><span>){
    </span><span>$str</span>=""<span>;
    </span><span>if</span>(<span>$root</span>)<span>$str</span> .= "<xml>"<span>;
    </span><span>foreach</span>(<span>$data</span><span>as</span><span>$key</span> => <span>$val</span><span>){
        </span><span>if</span>(<span>is_array</span>(<span>$val</span><span>)){
            </span><span>$child</span> = arr2xml(<span>$val</span>, <span>false</span><span>);
            </span><span>$str</span> .= "<<span>$key</span>><span>$child</span></<span>$key</span>>"<span>;
        }</span><span>else</span><span>{
            </span><span>$str</span>.= "<<span>$key</span>><![CDATA[<span>$val</span>]]></<span>$key</span>>"<span>;
        }
    }
    </span><span>if</span>(<span>$root</span>)<span>$str</span> .= "</xml>"<span>;
    </span><span>return</span><span>$str</span><span>;
}</span>

The above is the implementation method. The first parameter is the array you want to convert. The second optional parameter sets whether to add the root node. It is required by default.

Test code:

<span>$arr</span>=<span>array</span>('a'=>'aaa','b'=><span>array</span>('c'=>'1234' , 'd' => "asdfasdf"<span>));
</span><span>echo</span> arr2xml(<span>$arr</span>);

The result after code execution is:

<span><</span><span>xml</span><span>><</span><span>a</span><span>></span><span><!</span><span>[CDATA[aaa]]</span><span>></span><span></</span><span>a</span><span>><</span><span>b</span><span>><</span><span>c</span><span>></span><span><!</span><span>[CDATA[1234]]</span><span>></span><span></</span><span>c</span><span>><</span><span>d</span><span>></span><span><!</span><span>[CDATA[asdfasdf]]</span><span>></span><span></</span><span>d</span><span>></</span><span>b</span><span>></</span><span>xml</span><span>></span>

The above introduces the recursive implementation of converting PHP arrays to XML, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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
Previous article:php string function (2)Next article:php string function (2)