PHP对象转成数组

项目中跨平台调用数据,会用到json或xml。需要将json和xml对象转成数组。用下面的函数就可以了

/**
 * 
 * 把对象转成数组
 * @param $object 要转的对象$object 
 */
function objectToArray($object){  
    
    $result = array();  
    
    $object = is_object($object) ? get_object_vars($object) : $object;  
    
    foreach ($object as $key => $val) {  
        
        $val = (is_object($val) || is_array($val)) ? objectToArray($val) : $val;  
      
        $result[$key] = $val;  
 }  
 
    return $result;  
}

Comments are closed.