Home  >  Article  >  Backend Development  >  PHP implements mutual conversion between arrays and XML files

PHP implements mutual conversion between arrays and XML files

小云云
小云云Original
2018-03-16 09:47:331355browse

This article mainly introduces the mutual conversion function of arrays and XML files implemented by PHP, and analyzes PHP's operation skills for mutual conversion of xml format data and arrays in the form of examples. Friends who need it can refer to it. I hope it can help everyone.

Recently, WeChat payment is being made, and the WeChat server returns all XML files, so it needs to be converted into an array to facilitate operation. Without further ado, let’s go directly to the code:

1. Convert XML to array

/**
 * 将xml转为array
 * @param string  $xml xml字符串或者xml文件名
 * @param bool   $isfile 传入的是否是xml文件名
 * @return array  转换得到的数组
 */
function xmlToArray($xml,$isfile=false){
  //禁止引用外部xml实体
  libxml_disable_entity_loader(true);
  if($isfile){
    if(!file_exists($xml)) return false;
    $xmlstr = file_get_contents($xml);
  }else{
    $xmlstr = $xml;
  }
  $result= json_decode(json_encode(simplexml_load_string($xmlstr, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  return $result;
}

Usage example:

$xmlDoc=<<<ETO
<books>
 <book>
 <author>Jack Herrington</author>
 <title>PHP Hacks</title>
 <publisher>O'Reilly</publisher>
 </book>
 <book>
 <author>Jack Herrington</author>
 <title>Podcasting Hacks</title>
 <publisher>O'Reilly</publisher>
 </book>
 <book>
 <author>XML格式化</author>
 <title>脚本之家在线工具</title>
 <publisher>tools.jb51.net</publisher>
 </book>
</books>
ETO;
$relarr=xmlToArray($xmlDoc);
print_r($relarr);

Run result:

Array
(
  [book] => Array
    (
      [0] => Array
        (
          [author] => Jack Herrington
          [title] => PHP Hacks
          [publisher] => O'Reilly
        )
      [1] => Array
        (
          [author] => Jack Herrington
          [title] => Podcasting Hacks
          [publisher] => O'Reilly
        )
      [2] => Array
        (
          [author] => XML格式化
          [title] => 脚本之家在线工具
          [publisher] => tools.jb51.net
        )
    )
)

2. Convert array to XML

/**
 * 数组转xml字符
 * @param string  $xml xml字符串
**/
function arrayToXml($data){
  if(!is_array($data) || count($data) <= 0){
    return false;
  }
  $xml = "<xml>";
  foreach ($data as $key=>$val){
    if (is_numeric($val)){
      $xml.="<".$key.">".$val."</".$key.">";
    }else{
      $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
    }
  }
  $xml.="</xml>";
  return $xml;
}

Usage example:

$arrDoc= array("author"=>"XML格式化","title"=>"演示","publisher"=>"tools.jb51.net");
$xmlrel=arrayToXml($arrDoc);
//运行结果:<xml><author><![CDATA[XML格式化]]></author><title><演示]></title
><publisher><![CDATA[tools.jb51.net]]></publisher></xml>

Related recommendations:

php Processing example code for conversion between arrays and XML

The above is the detailed content of PHP implements mutual conversion between arrays and XML files. 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