Created
December 30, 2019 08:16
-
-
Save yusureabc/9dd6c0bd7dcf0aebb5bcb597275dba5b to your computer and use it in GitHub Desktop.
php XML Array 互转
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.">" . $val . "</".$key.">"; | |
} | |
} | |
$xml.="</xml>"; | |
return $xml; | |
} | |
//Xml转数组 | |
function XmlToArr($xml) | |
{ | |
if($xml == '') return ''; | |
libxml_disable_entity_loader(true); | |
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); | |
return $arr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment