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

Detailed explanation of conversion examples between PHP XML and arrays

墨辰丷
墨辰丷Original
2018-05-31 10:24:541288browse

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;
  }

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.

d0339ea0abeb4f1275ddb6c994f9f846


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!

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