php里Array2xml

<? php
class  Array2xml
{
    
var   $xml ;
    
function  array2xml( $array , $encoding = ' utf-8 ' ) {
        
$this -> xml = ' <?xml version="1.0" encoding=" ' . $encoding . ' "?> ' ;
        
$this -> xml .= $this -> _array2xml( $array );
    }
    
function  getXml() {
        
return   $this -> xml;
    }
    
function  _array2xml( $array )
    {
        
$xml = '' ;
        
foreach ( $array   as   $key => $val ){
            
if ( is_numeric ( $key )){
                
$key = " item id=\" $key \" " ;
            }
else {
                
// 去掉空格,只取空格之前文字为key
                 list ( $key , ) = explode ( '   ' , $key );
            } 
            
$xml .= " < $key > " ;
            
$xml .= is_array ( $val ) ? $this -> _array2xml( $val ) : $val ;
            
// 去掉空格,只取空格之前文字为key
             list ( $key , ) = explode ( '   ' , $key );
            
$xml .= " </ $key > " ;
        }
        
return   $xml ;
    }
}

你可能感兴趣的:(array)