Conversion between PHP Objects and arrays

Source: Internet
Author: User
Conversion between PHP Objects and arrays ** @ authorflyer0126 * @ since20120503 ** 1. use (array) and (object) to process $ objTemp (object) array (); $ objTemp-& amp; gt; a1; $ objTemp-& amp; gt; b2; $ objTemp-& amp; gt; c3 ;... /**

* Conversion between PHP Objects and arrays

*

* @ Author flyer0126

* @ Since 2012/05/03

**/


// 1. simple processing with (array) and (object)

$ ObjTemp = (object) array ();

$ ObjTemp-> a = 1;

$ ObjTemp-> B = 2;

$ ObjTemp-> c = 3;

$ ArrTemp = (array) $ objTemp;

Print_r ($ objTemp );

Print_r ($ arrTemp );


/**

StdClass Object

(

[A] => 1

[B] => 2

[C] => 3

)

Array

(

[A] => 1

[B] => 2

[C] => 3

)

**/


// PS: Simple (array) and (object) can only process single-layer data, but there is no way to convert arrays and objects in multiple layers.


// 2. convert between multi-dimensional arrays and objects


/**

* Convert an object to a multi-dimensional array

*

**/

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;

}

}

/**

* Convert a multi-dimensional array to an object

*

**/

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:

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


// Print objects and array

Print_r ($ init );

Print_r ($ array );

Print_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

)

)

[Foox] => Just test

)

StdClass Object

(

[Foo] => 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.