Home  >  Article  >  Backend Development  >  How to convert php array to xml

How to convert php array to xml

藏色散人
藏色散人Original
2020-09-04 09:33:042654browse

php array to xml method: first create a PHP sample file; then define an "arrayToXml" method; then use "is_numeric" and other functions to convert array to xml; and finally output the converted xml.

How to convert php array to xml

Recommended: "PHP Video Tutorial"

Code:

/**
 * 数组转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; 
}

The above is the detailed content of How to convert php array to xml. 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