// +---------------------------------------------------------------------- namespace crmeb\services\upload; /** * Class XML * @package crmeb\services\upload */ class XML { /** * XML 转数组 * @param string $xml * @return mixed */ public static function xmlToArray(string $xml) { $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $json = json_encode($xml); return json_decode($json, true); } /** * 数组转 XML * @param array $data * @return string */ public static function arrayToXml(array $data): string { $xml = ''; foreach ($data as $key => $val) { if (is_numeric($val)) { $xml .= '<' . $key . '>' . $val . ''; } else { $xml .= '<' . $key . '>'; } } $xml .= ''; return $xml; } }