PHP将XML文件转换成PHP数组

网络整理 - 08-30

<?php                                                    
$s=join("",file('test.xml'));                         
$result = xml_to_array($s);                              
                                                         
function xml_to_array($xml)                              
{                                                        
  $array = (array)(simplexml_load_string($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;                                          
}                                                        
print_r($result);                                        
?>