Detailed explanation of the steps for converting arrays and XML files into each other in PHP

php中世界最好的语言
Release: 2023-03-26 13:54:01
Original
1037 people have browsed it

This time I will bring you a detailed explanation of the steps for converting arrays and XML files implemented in PHP. What are the precautions for converting arrays and XML files implemented in PHP? Here are practical cases, let’s take a look. one time.

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;
}
Copy after login

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);
Copy after login

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
        )
    )
)
Copy after login

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;
}
Copy after login

Usage examples:

$arrDoc= array("author"=>"XML格式化","title"=>"脚本之家在线工具","publisher"=>"tools.jb51.net");
$xmlrel=arrayToXml($arrDoc);
//运行结果:<xml><author><![CDATA[XML格式化]]></author><title><![CDATA[脚本之家在线工具]]></title><publisher><![CDATA[tools.jb51.net]]></publisher></xml>
Copy after login

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the actual case of implementing WeChat payment with PHP

ThinkPHP detailed explanation of the steps to implement WeChat payment (jsapi payment)

The above is the detailed content of Detailed explanation of the steps for converting arrays and XML files into each other in PHP. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!