Home > Backend Development > PHP Tutorial > Convert php objects to arrays and arrays to objects_PHP tutorial

Convert php objects to arrays and arrays to objects_PHP tutorial

WBOY
Release: 2016-07-13 10:07:56
Original
892 people have browsed it

PHP object to array and array to object operations

1. The following is the code to convert an object into an array:

public static function object2array($d)
	{
		if (is_object($d)) $d = get_object_vars($d);
		if (is_array($d))
			return array_map('self::object2array', $d);
		else
			return $d;
	}
Copy after login

2. The following is the code for converting an array into an object:

public static function array2object($d)
	{
		if (is_array($d))
			return (object) array_map('self::array2object', $d);
		else
			return $d;
	}
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/953330.htmlTechArticlePHP object to array and array to object operations 1. The following is the code for converting an object to an array: public static function object2array($d){if (is_object($d)) $d = get_object_vars($d);if (i...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template