Torch light How many layers of Php simple object and array conversion function code PHP multi-layer Array and object conversion

Source: Internet
Author: User
Copy CodeThe code is as follows:


function Arraytoobject ($e) {
if (GetType ($e)! = ' array ') return;
foreach ($e as $k = = $v) {
if (GetType ($v) = = ' Array ' | | GetType ($v) = = ' object ')
$e [$k]= (object) Arraytoobject ($v);
}
Return (object) $e;
}
function Objecttoarray ($e) {
$e = (array) $e;
foreach ($e as $k = = $v) {
if (GetType ($v) = = ' resource ') return;
if (GetType ($v) = = ' object ' | | GetType ($v) = = ' array ')
$e [$k]= (Array) Objecttoarray ($v);
}
return $e;
}


The above content is from Cnblogs Jaiho
PHP multi-layer array and object conversions
Multi-layer arrays and object conversions are simple to handle the conversion of multiple arrays and objects in WebService
Simple (array) and (object) can only handle single-layer data, and there is nothing to do with multi-layered arrays and object conversions.
You can convert an object to an array one time through Json_decode (the $object), but there is a problem with non-ASCII characters that are not utf-8 encoded in object, such as GBK's Chinese, not to mention Json_ The performance of encode and decode is also questionable.
The following code:

Copy the Code code as follows:


function Objecttoarray ($d) {
if (Is_object ($d)) {
Gets the properties of the given object
With Get_object_vars function
$d = Get_object_vars ($d);
}
if (Is_array ($d)) {
/*
* Return Array converted to object
* Using __function__ (Magic constant)
* For Recursive call
*/
Return Array_map (__function__, $d);
}
else {
Return Array
return $d;
}
}
function Arraytoobject ($d) {
if (Is_array ($d)) {
/*
* Return Array converted to object
* Using __function__ (Magic constant)
* For Recursive call
*/
Return (object) Array_map (__function__, $d);
}
else {
Return Object
return $d;
}
}
Useage:
Create New StdClass Object
$init = new StdClass;
ADD some test data
$init->foo = "Test data";
$init->bar = new StdClass;
$init->bar->baaz = "testing";
$init->bar->fooz = new StdClass;
$init->bar->fooz->baz = "Testing Again";
$init->foox = "Just test";
Convert array to object and then object back to array
$array = Objecttoarray ($init);
$object = Arraytoobject ($array);
Print objects and Array
Print_r ($init);
echo "\ n";
Print_r ($array);
echo "\ n";
Print_r ($object);
?>


The above describes how many layers of the light of the Torch, PHP simple objects and arrays of conversion function code PHP multi-layered array and object conversion, including the light of the torch how many layers of content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.