Transformations between PHP objects and arrays

Source: Internet
Author: User
Conversion between PHP objects and arrays
/** * PHP objects, conversions between arrays * * @author flyer0126 * @since 2012/05/03 **///1. using (array) and (object), simple processing $objtemp = (object) array (), $objTemp->a = 1; $objTemp->b = 2; $objTemp->c = 3; $arrTemp = ( Array) $objTemp;p rint_r ($objTemp);p rint_r ($arrTemp);?  /**stdclass Object ([A] = 1 [b] = 2 [c] + 3) Array ([A] = 1 [b] = 2 [c] = 3) **/// PS: Simple (array) and (object) can only handle single-layer data, for multi-layered array and object conversion is powerless. 2. Conversion processing between multidimensional arrays and objects/** * Convert an object to a multidimensional array * **/function Objecttoarray ($d) {if (Is_object ($d)) {//Gets the-the-given obj ect//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 a Rray_map (__function__, $d);} else {//Return Arrayreturn $d;}} /** * Convert multidimensional arrays to Objects * **/function Arraytoobject ($d) {if (Is_array ($d)) {/** Return array converted to object* Using __functio N__ (Magic constant) * For recursive Call*/return (object) Array_map (__function__, $d);} Else {//Return Objectreturn $d;}} Useage: $init = new StdClass; $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);//Prin T objects and Arrayprint_r ($init);p rint_r ($array);p rint_r ($object);?            /**stdclass object ([foo] = Test data [bar] = StdClass object ([Baaz] = Testing     [Fooz] = StdClass Object ([baz] = testing again))            [Foox] = Just test) array ([foo] = test data [Bar] = = Array ([Baaz] = Testing [Fooz] = = Array ([baz] = testing again)) [F Oox] = Just test) stdClass Object ([foo] =&Gt                Test data [Bar] = StdClass object ([Baaz] = testing [Fooz] = StdClass Object    ([Baz] = testing again)) [Foox] = Just test) **/
  • 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.