Skip to content

Instantly share code, notes, and snippets.

@yusureabc
Created December 30, 2019 08:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusureabc/9dd6c0bd7dcf0aebb5bcb597275dba5b to your computer and use it in GitHub Desktop.
Save yusureabc/9dd6c0bd7dcf0aebb5bcb597275dba5b to your computer and use it in GitHub Desktop.
php XML Array 互转
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