Home  >  Article  >  Backend Development  >  PHP array generates Xml format

PHP array generates Xml format

不言
不言Original
2018-05-08 13:53:551374browse

This article mainly introduces the Xml format generated by PHP arrays, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Methods to generate Xml when the public account is developed

    public  function arrayToXml($arr){
        $xml = "<root>";        
        foreach ($arr as $key=>$val){            
        if(is_array($val)){                
        $xml.="<".$key.">".arrayToXml($val)."</".$key.">";
            }else{                
            $xml.="<".$key.">".$val."</".$key.">";
            }
        }       
       $xml.="</root>";        
       return $xml;
    }

xml converted to array

function xmlToArray($xml,$isfile=false){     
    //禁止引用外部xml实体  
    libxml_disable_entity_loader(true);  
    if($isfile){  
        if(!file_exists($xml)) return false;  
        $xmlstr = file_get_contents($xml);  
    }else{  
        $xmlstr = $xml;  
    }  
    $result= json_decode(json_encode(simplexml_load_string($xmlstr, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA)), true);          
    return $result;  
}

Related recommendations:

Code to generate CSV file from a PHP array

The above is the detailed content of PHP array generates Xml format. For more information, please follow other related articles on the PHP Chinese website!

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