How to convert php array to xml

藏色散人
Release: 2023-03-08 12:14:01
Original
3159 people have browsed it

How to convert php array to xml: first create a PHP sample file; then create an arrayToXml method; finally pass "$xml.="<".$key.">< in the method body ![CDATA[".$val."]]>";" 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;
}
Copy after login

[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!

Related labels:
php
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