·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> php数组转xml的递归实现

php数组转xml的递归实现

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
php数组转xml的递归实现

PHP中奖数组转为xml的需求是常见的,而且实现方法也有很多种,百度找了一下各种实现方法,但是基本是借组一些组件啥的。我就自己写了一个字符串拼组的方法,支持多维数组。仅供参考,不足之处敬请不吝赐教!

/***   将数组转换为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 .= "<xml>";    foreach($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;}

上面是实现的方法,第一个参数是你要转换的数组,第二个可选参数设定是否需要加<xml>根节点,默认是需要的。

测试代码:

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

代码执行后的结果为:

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

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

更新:

在使用过程中发现下面格式的数组转换会出现问题:

array(    'item' => array(        array(            'title' => 'qwe',            'description' => 'rtrt',            'picurl' => 'sdfsd',            'url' => 'ghjghj'        ),        array(            'title' => 'jyutyu',            'description' => 'werwe',            'picurl' => 'xcvxv',            'url' => 'ghjgh'        )    ));

转换出来的结果是:

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

通常情况下,上面转换出来的xml整<0><1>那层节点我们是不要的。但是在php中下标有不能同名,不能有多个item。怎么办呢?

我想了一个办法就是给item下下标,比如item[0],item[1],在转换过程中在去掉[]形式的下标,实现多个item节点并排。

函数修改后如下:

function arr2xml($data, $root = true){    $str="";    if($root)$str .= "<xml>";    foreach($data as $key => $val){        //去掉key中的下标[]        $key = PReg_replace('/\[\d*\]/', '', $key);        if(is_array($val)){            $child = arr2xml($val, false);            $str .= "<$key>$child</$key>";        }else{            $str.= "<$key><![CDATA[$val]]></$key>";        }    }    if($root)$str .= "</xml>";    return $str;}

那么上面需要转换的数组也需要跟着变动一下:

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

转换后的xml如下:

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