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

Detailed explanation of conversion between arrays and XML in PHP

墨辰丷
墨辰丷Original
2018-06-02 09:18:521314browse

This article mainly introduces how to use PHP to process the conversion between arrays and XML. It introduces in detail how PHP converts XML into arrays and how PHP converts arrays into XML. Interested friends can refer to it.

In development, we often encounter the conversion between arrays and XML, especially when dealing with interface development. For example, the other client POSTs data in XML format to the server, and the server The program on it is responsible for receiving and parsing, and also needs to provide the data table data to third-party applications in XML format.

PHP converts the array into XML
PHP can convert the array into xml format. The simple way is to traverse the array and then convert the array's key/value Convert it into an xml node, and then directly echo the output, such as:

function arrayToXml($arr){ 
$xml = "<root>"; 
foreach ($arr as $key=>$val){ 
if(is_array($val)){ 
$xml.="<".$key.">".arrayToXml($val)."</".$key.">"; 
}else{ 
$xml.="<".$key.">".$val."</".$key.">"; 
} 
} 
$xml.="</root>"; 
return $xml; 
}

I tested it, this is the simplest, fast, and supports many As an array, Chinese characters will not be garbled.
Another method is to use DOMDocument to generate xml structure:

function arrayToXml($arr,$dom=0,$item=0){ 
if (!$dom){ 
$dom = new DOMDocument("1.0"); 
} 
if(!$item){ 
$item = $dom->createElement("root"); 
$dom->appendChild($item); 
} 
foreach ($arr as $key=>$val){ 
$itemx = $dom->createElement(is_string($key)?$key:"item"); 
$item->appendChild($itemx); 
if (!is_array($val)){ 
$text = $dom->createTextNode($val); 
$itemx->appendChild($text); 

}else { 
arrayToXml($val,$dom,$itemx); 
} 
} 
return $dom->saveXML(); 
}

It can also convert arrays into xml and supports multi-dimensional arrays , the generated xml Chinese will not be garbled.

PHP converts XML into an array
When doing interface development, you often encounter data submitted to you by others in xml format, such as common WeChat interfaces, Alipay interfaces, etc. Their interfaces such as sending messages and communicating are all in XML format, so we first find a way to get this XML data, and then convert it into an array.
Suppose we get an XML like this:

 <root> 

<user>

月光光abcd</user> 

<pvs>13002</pvs>

 <ips> 

<baidu_ip>1200</baidu_ip>

 <google_ip>1829</google_ip>

 </ips> 

<date>2016-06-01</date>

 </root>

Parse and read the xml data through simplexml_load_string(), and then convert it to json format first , and then converted into an array.

 function xmlToArray($xml){ 

 //禁止引用外部xml实体 

libxml_disable_entity_loader(true); 

$xmlstring = simplexml_load_string($xml, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA); 

$val = json_decode(json_encode($xmlstring),true); 

return $val; 

}

Call xmlToArray() to get the following results:


After getting the array, we can perform various processing on the data.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

Based on php to implement the use of QR codes with parameters in WeChat development

How to use PHP to implement regular crawling of URLs in pages

php jQuery Ajax to implement asynchronous page refresh function

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