Home >php教程 >php手册 >PHP对象转成数组

PHP对象转成数组

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 09:37:381361browse

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;  
}


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn