PHP将XML文件转换成PHP数组

 

<?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);                                        
?>        

posted @ 2015-01-28 18:57  dcj890828  阅读(130)  评论(0编辑  收藏  举报