Several methods to convert stdClass Object to array in PHP_PHP Tutorial

WBOY
Release: 2016-07-13 10:30:13
Original
854 people have browsed it

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...

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