PHP将XML文件转换成PHP数组

前提是有一个test.xml文件

<?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 @ 2011-01-12 15:29  春哥也编程  阅读(7195)  评论(0编辑  收藏  举报