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 Conversion
Multi-layer arrays and object conversion are easy to use, making it easy to process multi-layer arrays and object conversion in WebService.
Simple (array) and (object) can only process single-layer data, but there is nothing to do with multi-layer array and object conversion.
Json_decode (json_encode ($ object) can be used to convert an object to an array at a time. However, problems may occur when the object encounters non-UTF-8 encoded non-ascii characters, such as gbk Chinese characters, besides, the performance of json_encode and decode is also worth considering.

The code below:
Copy codeThe Code is as follows:
<? Php

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 );
?>

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.