PHP array generates Xml format

不言
Release: 2023-03-25 16:30:01
Original
1380 people have browsed it

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

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

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!