Home > Backend Development > PHP Tutorial > Function to convert PHP array to XML

Function to convert PHP array to XML

不言
Release: 2023-03-24 13:38:01
Original
1602 people have browsed it

This article mainly introduces the functions for converting PHP arrays and XML. It has certain reference value. Now I share it with everyone. Friends in need can refer to it

//数组转xml
function ArrToXml($arr)
{
if(!is_array($arr) || count($arr) == 0) return '';
$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转数组
function XmlToArr($xml)
{
if($xml == &#39;&#39;) return &#39;&#39;;
libxml_disable_entity_loader(true);
$arr = json_decode(json_encode(simplexml_load_string($xml, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA)), true);
return $arr;
}
Copy after login

Related recommendations:

php array function sequence array_pop() deletes the last element in the array

The above is the detailed content of Function to convert PHP array to XML. 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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template