Several methods of transferring Stdclass object to array in PHP

Source: Internet
Author: User
Tags array example

method One: Copy the code code as follows://PHP StdClass Object goto Arrayfunction 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 Two: Copy the Code code as follows: $array=Json_decode (Json_encode (simplexml_load_string ($xmlString)), TRUE); method Three: Copy the Code code 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 amount of data is 10W, the implementation of the 1s, the structure is more complex, can achieve 3s, poor performance can be replaced with the following: Copy code code as follows: function Object2array (&$Object) {             $Object= Json_decode (Json_encode ($Object),true); return$Object; However, the JSON feature is only for UTF8, otherwise it has to be transcoded first.




PHP object to array example


function Std_class_object_to_array ($stdclassobject)
{
$_array = Is_object ($stdclassobject)? Get_object_vars ($stdclassobject): $stdclassobject;

foreach ($_array as $key = = $value) {
$value = (Is_array ($value) | | is_object ($value))? Std_class_object_to_array ($value): $value;
$array [$key] = $value;
}

return $array;
}

Several methods of transferring Stdclass object to array in PHP

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.