Home > Backend Development > PHP Tutorial > PHP handles conversion between arrays and XML, php handles array xml_PHP tutorial

PHP handles conversion between arrays and XML, php handles array xml_PHP tutorial

WBOY
Release: 2016-07-12 08:51:01
Original
780 people have browsed it

PHP handles the conversion between arrays and XML, and php handles array xml

In development, we often encounter the conversion between arrays and XML, especially in It is often used when dealing with interface development. For example, the other client POSTs data in XML format to the server. The program on the server is responsible for receiving and parsing, and the data table data needs to be provided to third-party applications in XML format.
In this article we will briefly introduce how to use PHP to handle conversion between arrays and XML.

Source code download: Conversion between PHP array and XML

PHP converts array to XML
PHP can convert the array into xml format. The simple way is to traverse the array, then convert the key/value of the array into xml nodes, and then echo the output directly, 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; 
}
Copy after login

I tested it, this is the simplest, fast, supports mostly arrays, and 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)&#63;$key:"item"); 
$item->appendChild($itemx); 
if (!is_array($val)){ 
$text = $dom->createTextNode($val); 
$itemx->appendChild($text); 

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

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

PHP converts XML into array
When doing interface development, you often encounter data submitted to you by others in XML format. Common WeChat interfaces, Alipay interfaces, etc., their interfaces such as sending messages and communication are all in XML format, so let’s find a way first 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> 

Copy after login

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

 function xmlToArray($xml){ 

 //禁止引用外部xml实体 

libxml_disable_entity_loader(true); 

$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); 

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

return $val; 

} 
Copy after login

Call xmlToArray() to get the following results:


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

The above is the entire content of this article. I hope it will be helpful to everyone’s study. I also hope that everyone will support Bangke Home.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133030.htmlTechArticlePHP handles the conversion between arrays and XML, and php handles array xml. In development, we often encounter Conversion between arrays and XML is often used especially when dealing with interface development...
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