Several methods of Stdclass object to array in PHP _php instance

Source: Internet
Author: User

Method One:

Copy Code code 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);
}
}
return $array;
}

Method Two:

Copy Code code as follows:

$array = Json_decode (Json_encode (simplexml_load_string ($xmlString)), TRUE);

Method Three:

Copy 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 it is 10W of data, the implementation to 1s, the structure more complex, can reach 3s, poor performance
You can replace it with the following:
Copy Code code as follows:
Function Object2array (& $object) {
$object = Json_decode (Json_encode ($object), true);
return $object;
}

But the characteristics of JSON, can only be directed at UTF8, otherwise you have to first transcoding.

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.