Home > Backend Development > PHP Tutorial > Recursive implementation of converting php array to xml, php array xml recursive_PHP tutorial

Recursive implementation of converting php array to xml, php array xml recursive_PHP tutorial

WBOY
Release: 2016-07-13 09:53:18
Original
983 people have browsed it

Recursive implementation of php array to xml, php array xml recursion

The need to convert PHP winning array to xml is common, and there are many implementation methods. Baidu searched There are various implementation methods, but basically they are borrowing some components and so on. 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>
Copy after login

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>);
Copy after login

The result after executing the code 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>
Copy after login

---------------------- ----------

Update:

During use, it was discovered that there would be problems with array conversion in the following format:

<span>array</span><span>(
    </span>'item' => <span>array</span><span>(
        </span><span>array</span><span>(
            </span>'title' => 'qwe',
            'description' => 'rtrt',
            'picurl' => 'sdfsd',
            'url' => 'ghjghj'<span>
        )</span>,
        <span>array</span><span>(
            </span>'title' => 'jyutyu',
            'description' => 'werwe',
            'picurl' => 'xcvxv',
            'url' => 'ghjgh'<span>
        )
    )
);</span>
Copy after login

The converted result is:

<span><</span><span>xml</span><span>></span>
    <span><</span><span>item</span><span>></span>
        <span><</span><span>0</span><span>></span>
            <span><</span><span>title</span><span>></span>
                <span><![CDATA[</span><span>qwe</span><span>]]></span>
            <span></</span><span>title</span><span>></span>
            <span><</span><span>description</span><span>></span>
                <span><![CDATA[</span><span>rtrt</span><span>]]></span>
            <span></</span><span>description</span><span>></span>
            <span><</span><span>picurl</span><span>></span>
                <span><![CDATA[</span><span>sdfsd</span><span>]]></span>
            <span></</span><span>picurl</span><span>></span>
            <span><</span><span>url</span><span>></span>
                <span><![CDATA[</span><span>ghjghj</span><span>]]></span>
            <span></</span><span>url</span><span>></span>
        <span></</span><span>0</span><span>></span>
        <span><</span><span>1</span><span>></span>
            <span><</span><span>title</span><span>></span>
                <span><![CDATA[</span><span>jyutyu</span><span>]]></span>
            <span></</span><span>title</span><span>></span>
            <span><</span><span>description</span><span>></span>
                <span><![CDATA[</span><span>werwe</span><span>]]></span>
            <span></</span><span>description</span><span>></span>
            <span><</span><span>picurl</span><span>></span>
                <span><![CDATA[</span><span>xcvxv</span><span>]]></span>
            <span></</span><span>picurl</span><span>></span>
            <span><</span><span>url</span><span>></span>
                <span><![CDATA[</span><span>ghjgh</span><span>]]></span>
            <span></</span><span>url</span><span>></span>
        <span></</span><span>1</span><span>></span>
    <span></</span><span>item</span><span>></span>
<span></</span><span>xml</span><span>></span>
Copy after login

Normally, we don’t want the whole layer of nodes in the xml converted above: <0><1>. However, in php, the subscript cannot have the same name and cannot have multiple items. What to do?

I thought of a way to subscript items, such as item[0], item[1], and remove the subscripts in the form of [] during the conversion process to achieve multiple item nodes side by side.

The modified function is as follows:

<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>//</span><span>去掉key中的下标[]</span>
        <span>$key</span> = <span>preg_replace</span>('/\[\d*\]/', '', <span>$key</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>
Copy after login

Then the array that needs to be converted above also needs to be changed accordingly:

<span>$arr1</span> =<span>array</span><span>(
    </span>'item[0]' => <span>array</span><span>(
            </span>'title' => 'qwe',
            'description' => 'rtrt',
            'picurl' => 'sdfsd',
            'url' => 'ghjghj'<span>
        )</span>,
    'item[1]' => <span>array</span><span>(
            </span>'title' => 'jyutyu',
            'description' => 'werwe',
            'picurl' => 'xcvxv',
            'url' => 'ghjgh'<span>
        )
);</span>
Copy after login

The converted xml is as follows:

<span><</span><span>xml</span><span>></span>
    <span><</span><span>item</span><span>></span>
        <span><</span><span>title</span><span>></span>
            <span><![CDATA[</span><span>qwe</span><span>]]></span>
        <span></</span><span>title</span><span>></span>
        <span><</span><span>description</span><span>></span>
            <span><![CDATA[</span><span>rtrt</span><span>]]></span>
        <span></</span><span>description</span><span>></span>
        <span><</span><span>picurl</span><span>></span>
            <span><![CDATA[</span><span>sdfsd</span><span>]]></span>
        <span></</span><span>picurl</span><span>></span>
        <span><</span><span>url</span><span>></span>
            <span><![CDATA[</span><span>ghjghj</span><span>]]></span>
        <span></</span><span>url</span><span>></span>
    <span></</span><span>item</span><span>></span>
    <span><</span><span>item</span><span>></span>
        <span><</span><span>title</span><span>></span>
            <span><![CDATA[</span><span>jyutyu</span><span>]]></span>
        <span></</span><span>title</span><span>></span>
        <span><</span><span>description</span><span>></span>
            <span><![CDATA[</span><span>werwe</span><span>]]></span>
        <span></</span><span>description</span><span>></span>
        <span><</span><span>picurl</span><span>></span>
            <span><![CDATA[</span><span>xcvxv</span><span>]]></span>
        <span></</span><span>picurl</span><span>></span>
        <span><</span><span>url</span><span>></span>
            <span><![CDATA[</span><span>ghjgh</span><span>]]></span>
        <span></</span><span>url</span><span>></span>
    <span></</span><span>item</span><span>></span>
<span></</span><span>xml</span><span>></span>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1001463.htmlTechArticleRecursive implementation of converting PHP array to xml, PHP array xml recursion The need to convert PHP winning array to xml is common, And there are many implementation methods. Baidu searched for various implementation methods, but basically...
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template