美文网首页
php-xml<=>array互转

php-xml<=>array互转

作者: 若初_终点 | 来源:发表于2020-04-04 10:56 被阅读0次

............................................................................................................................................................................................................................................

    //数组转XML
    function arrayToXml($arr)
    {
        $xml = "<xml>";
        foreach ($arr as $key=>$val)
        {
            if (is_numeric($val)){
                $xml.="<".$key.">".$val."</".$key.">";
            }else{
                 $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
            }
        }
        $xml.="</xml>";
        return $xml;
    }

    //将XML转为array
    function xmlToArray($xml)
    {    
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);        
        return $values;
    }
    
    
............................................................................................................................................................................................................................................    

相关文章

网友评论

      本文标题:php-xml<=>array互转

      本文链接:https://www.haomeiwen.com/subject/iqmdphtx.html