simplexml_load_string从字符串中获取
你提供的是打开文件的链接,并不是字符串.
$file ="data.xml";
$content = file_get_contents($file);
$a=xml_to_array($content);
dump($a);
你提供的是打开文件的链接,并不是字符串.
$file ="data.xml";
$content = file_get_contents($file);
$a=xml_to_array($content);
dump($a);
把simplexml_load_string改为simplexml_load_file,加载文件,这就可以了,不用file_get_contents用一个函数搞定两个功能,代码(只改xml_to_array的函数)
<?phpfunction xml_to_array($xml)
{
$array = (array)(simplexml_load_file($xml));
foreach ($array as $key=>$item){
$array[$key] = struct_to_array((array)$item);
}
return $array;
}function struct_to_array($item) {
if(!is_string($item)) {
$item = (array)$item;
foreach ($item as $key=>$val){
$item[$key] = struct_to_array($val);
}
}
return $item;
}
$file ="data.xml";
$a=xml_to_array($file);
dump($a);
?>
这样就可以了