Several methods for converting stdClassObject to array in PHP. Method 1: Copy the code as follows: PHPstdClassObject to arrayfunctionobject_array ($ array) {if (is_object ($ array) {$ array (array) $ array ;} if (is_array ($ array) {forea
Method 1:
The code is as follows:
// Convert 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 );
}
}
Return $ array;
}
Method 2:
The code is as follows:
$ Array = json_decode (json_encode (simplexml_load_string ($ xmlString), TRUE );
Method 3:
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 );
}
}
Return $ arr;
}
If the data volume is 10 million, the execution should be 1 s, and the structure should be more complex. it can reach 3 s, resulting in poor performance.
You can replace it with the following:
The code is as follows:
Function object2array (& $ object ){
$ Object = json_decode (json_encode ($ object), true );
Return $ object;
}
However, the json feature can only be for utf8. otherwise, transcoding is required first.
Compile code: // PHP stdClass Object to convert array function object_array ($ array) {if (is_object ($ array) {$ array = (array) $ array ;} if (is_array ($ array) {forea...