/** *Author zhudongchang *Date 2015/6/12 *xml 转array 函数 *@param string $xmlStr xml字符串 *@return string */ function convertXmlToArray($xmlStr){ $patten=<<<EOT /(^\s*<(\s*[a-zA-Z0-9_=‘":]+\s*)+>.*<\s*\/(\s*[a-zA-Z0-9_=‘":]+\s*)+>\s*$)|(^\s*<(\s*[a-zA-Z0-9_=‘":]+\s*)+\/\s*>$)/s EOT; $pattenChildXml=<<<EOT /^\s*<(\s*[a-zA-Z0-9_=‘":]+\s*)+>(.*)<\s*\/(\s*[a-zA-Z0-9_=‘":]+\s*)+>\s*$/s EOT; $res=array(); $xml = simplexml_load_string($xmlStr); foreach($xml->children() as $val){ $key=$val->getName(); $valXml=$val->asXML(); preg_match($pattenChildXml,$valXml,$match); $childXmlStr=$match[2]; $temp=preg_match($patten,$childXmlStr)?convertXmlToArray($valXml):(string)$val; if(array_key_exists($key,$res)){ is_array($res[$key]) or $res[$key]=array($res[$key]); $res[$key]=array_merge($res[$key],$temp); }else{ $res[$key]=$temp; } } return $res; }
时间: 2024-10-14 08:07:56