php将数组转化成xml和json

在PHP中,大家一般操作数组的比较多。但有时,在做后台API或者其他情况下,需要同其他平台交换数据。一般常见的有XML,JSON两种!

1:数组转换成XML

function array2xml($array, $tag) { 

    function ia2xml($array) { 
        $xml=""; 
        foreach ($array as $key=>$value) { 
            if (is_array($value)) { 
                $xml.="<$key>".ia2xml($value)."</$key>"; 
            } else { 
                $xml.="<$key>".$value."</$key>"; 
            } 
        } 
        return $xml; 
    } 

    return simplexml_load_string("<$tag>".ia2xml($array)."</$tag>"); 
} 

2:数组转换成JSON,直接json_encode方法即可

另外多说一句,有的时候需要将object数据转化成数组。可以通过先将此数据json_encode,然后在解码json_decode 的时候,将第二个参数设置为true即可!

Heels are good
gay porn post awards and fashion recap TV specials

Immersing patrons in a magical shroud of style and
pornoObjects Of Fashion Decoration For Men
milf porn
此条目发表在 网站开发 分类目录。将固定链接加入收藏夹。

评论功能已关闭。