Home  >  Article  >  Backend Development  >  Several methods to convert stdClass Object to array in PHP_PHP Tutorial

Several methods to convert stdClass Object to array in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:30:13852browse

Method 1:

Copy code The code is as follows:

//PHP stdClass Object to array
function object_array($array) {
if(is_object($array)) {
$array = (array)$array;
} if(is_array($array)) {
foreach($array as $key=>$ value) {
              $array[$key] = object_array($value);                                            

Method 2:


Copy code The code is as follows:

$array = json_decode(json_encode(simplexml_load_string($xmlString)),TRUE);
Method 3:


Copy code
The code is as follows:

function object2array_pre( &$object) { if (is_object($object)) { $arr = (array)($object); } else { $arr = &$object;
}
if (is_array($arr)) {
foreach($arr as $varName => $varValue){
$arr[$varName] = $this->object2array($varValue) ;
                                                                                                                                                                 ; If
is bad, you can replace it with the following:



Copy the code

The code is as follows:

function object2array(&$object) {
$ Object = JSON_DECODE (json_encode ($ Object), TRUE);
Return $ Object;
}
But the characteristics of JSON can only be targeted at UTF8, otherwise you must first transfer it. .
http://www.bkjia.com/PHPjc/767088.html
www.bkjia.com

true

http: //www.bkjia.com/PHPjc/767088.html

TechArticleMethod 1: Copy the code as follows: //PHP stdClass Object to array function object_array($array) { if( is_object($array)) { $array = (array)$array; } if(is_array($array)) { forea...

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