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

Detailed explanation of conversion examples between PHP XML and arrays

墨辰丷
Release: 2023-03-28 15:56:02
Original
1354 people have browsed it

This article mainly introduces relevant information on the detailed explanation of the mutual conversion between PHP XML and arrays. Friends in need can refer to the following

PHP XML and array mutual 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 the XML file:


##>greater than number&&and''apostrophe""Double quotes The above is the entire content of this article, I hope it will be helpful to everyone's study.

<

##<

Less than number

>


Related recommendations:

Detailed explanation of how to change the hosts file in PHP


##PHP Method to implement asynchronous execution of script


PHP method to implement regular replacement of punctuation marks with spaces


The above is the detailed content of Detailed explanation of conversion examples between PHP XML and arrays. 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