Several methods for converting stdClassObject to array in PHP _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.