php把xml数据转化为数组
2021-04-15 本文已影响0人
风度翩翩的程序猿
//将 xml数据转换为数组格式。
private function xml_to_array($xml)
{
$reg = "/<(\w+)[^>]*>([\\x00-\\xFF]*)<\\/\\1>/";
if (preg_match_all($reg, $xml, $matches)) {
$count = count($matches[0]);
for ($i = 0; $i < $count; $i++) {
$subxml = $matches[2][$i];
$key = $matches[1][$i];
if (preg_match($reg, $subxml)) {
$arr[$key] = $this->xml_to_array($subxml);
} else {
$arr[$key] = $subxml;
}
}
}
return $arr;
}