";" Just convert the array into xml."/> ";" Just convert the array into xml.">

Home  >  Article  >  Backend Development  >  How to convert php array to xml

How to convert php array to xml

藏色散人
藏色散人Original
2021-03-03 10:08:103079browse

How to convert php array to xml: first create a PHP sample file; then create an arrayToXml method; finally pass "$xml.="d34df75372f4a53fbb2bbc0f061a0b1b97124ee9a9ee721ef98319cc2ceab842a0cd70d8ea9baf105402a6df3ab5b85c";" Just convert the array into xml.

How to convert php array to xml

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.

Convert php array to XML format, and convert XML format to array

//数组转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, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA)), true);
    return $values;
}

[Recommended learning: PHP video tutorial]

The above is the detailed content of How to convert php array to xml. 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