Home > Backend Development > PHP Tutorial > Detailed explanation of conversion between PHP XML and array

Detailed explanation of conversion between PHP XML and array

高洛峰
Release: 2023-03-04 06:08:01
Original
1571 people have browsed it

PHP XML and array conversion

//数组转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


Five predefined entities in XML files:

Detailed explanation of conversion between PHP XML and array

Thanks for reading, I hope it can help everyone, thank you for your support of this site!

For more detailed explanations of conversion between PHP XML and arrays, please pay attention to the PHP Chinese website!


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