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

PHP对象转成数组

WBOY
Release: 2016-06-13 09:37:38
Original
1337 people have browsed it

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;  
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template