将simplexml对象转换为数组结构

<?php
/**
* convert simplexml object to array sets
* $array_tags 表示需要转为数组的 xml 标签。例:array('item', '')
* 出错返回False
*
* @param object $simplexml_obj
* @param array $array_tags
* @param int $strip_white 是否清除左右空格
* @return mixed
*/
function simplexml_to_array($simplexml_obj, $array_tags=array(), $strip_white=1)
{    
    if( $simplexml_obj )
    {
        if( count($simplexml_obj)==0 )
            return $strip_white?trim((string)$simplexml_obj):(string)$simplexml_obj;
 
        $attr = array();
        foreach ($simplexml_obj as $k=>$val) {
            if( !empty($array_tags) && in_array($k, $array_tags) ) {
                $attr[] = simplexml_to_array($val, $array_tags, $strip_white);
            }else{
                $attr[$k] = simplexml_to_array($val, $array_tags, $strip_white);
            }
        }
        return $attr;
    }
 
    return FALSE;
}
php/simplexml_to_array.txt · 最后更改: 2010/04/30 15:40 由 admin
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki