Created
March 30, 2016 13:42
-
-
Save lbp0200/befd2550831913392fc66a7dc09df2c0 to your computer and use it in GitHub Desktop.
[PHP]数组与XML互转
This file contains hidden or 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
//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