Skip to content

Instantly share code, notes, and snippets.

@lbp0200
Created March 30, 2016 13:42
Show Gist options
  • Save lbp0200/befd2550831913392fc66a7dc09df2c0 to your computer and use it in GitHub Desktop.
Save lbp0200/befd2550831913392fc66a7dc09df2c0 to your computer and use it in GitHub Desktop.
[PHP]数组与XML互转
//xml to array
$resultXml = new SimpleXMLElement($result);
if (!$resultXml)
return ['status' => false, 'msg' => '微信支付订单生成出错'];
$resultArr = [];
foreach ($resultXml as $key => $value) {
$resultArr[$key] = strval($value);
}
/**
* 数组转XML
* @return SimpleXMLElement $xml
*/
public static function array2xml($data, $root = null)
{
$xml = new SimpleXMLElement($root ? '<' . $root . '/>' : '<root/>');
array_walk_recursive($data, function ($value, $key) use ($xml) {
$xml->addChild($key, $value);
});
return $xml->asXML();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment