Home > Backend Development > PHP Tutorial > PHP object to array (Object to Array), Json to array (Json to Array) method_PHP tutorial

PHP object to array (Object to Array), Json to array (Json to Array) method_PHP tutorial

WBOY
Release: 2016-07-13 10:16:27
Original
2611 people have browsed it

PHP object to array (Object to Array), Json to array (Json to Array) method

(1) PHP object to array method (object to array) array):

/**
 * object 转 array
 */
function object_to_array($obj){
	$_arr=is_object($obj)?get_object_vars($obj):$obj;
	foreach($_arr as $key=>$val){
		$val=(is_array($val))||is_object($val)?object_to_array($val):$val;
		$arr[$key]=$val;
	}
	return $arr;
}
Copy after login

(2) How to convert php Json character to array (json to array):

If it is a json string, you can directly convert the string into an array through the json_decode function.

json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0)

json_decode — accepts a JSON formatted string and converts it into a PHP variable

mixed json_decode(string $json[,bool $assoc])

Parameters:

json The string in json string format to be decoded.

assoc When this parameter is TRUE, the result in the form of an array (associative array) will be returned. The default is false and an object is returned.

Articles you may be interested in

  • How to convert a multi-dimensional array into a one-dimensional array in PHP
  • How to submit data using curl's post in PHP Summary of methods to obtain web page data with get
  • Convert Object to String function code in js
  • Use PHP function memory_get_usage to obtain the current PHP memory consumption to optimize program performance
  • php method to get the value of the first array unit of an array
  • php finds whether a value exists in the array (in_array(), array_search(), array_key_exists())
  • PHP array function array_map() notes
  • PHP’s functions urlencode(), urldecode(), rawurlencode(), rawurldecode()

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897780.htmlTechArticlePHP object to array (Object to Array), Json to array (Json to Array) method (1) php Method of converting object to array (object to array): /*** object to array*/function object_to_arra...
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