This article describes how to convert php Objects and arrays. it is very simple and practical to convert objects and arrays through two custom functions, for more information about how to convert php Objects and arrays, see the following example. Share it with you for your reference. The specific analysis is as follows:
Here we define two functions for converting php anonymous objects and arrays. the code is as follows:
Function array2object ($ array) {if (is_array ($ array) {$ obj = new StdClass (); foreach ($ array as $ key => $ val) {$ obj-> $ key = $ val;} else {$ obj = $ array;} return $ obj;} function object2array ($ object) {if (is_object ($ object) {foreach ($ object as $ key => $ value) {$ array [$ key] = $ value ;}} else {$ array = $ object;} return $ array ;}
Example:
$ Array = array ('foo' => 'bar', 'one' => 'two', 'Three '=> 'Four '); $ obj = array2object ($ array); print $ obj-> one; // output's "two" $ arr = object2array ($ obj ); print $ arr ['foo']; // output's bar
I hope this article will help you with php programming.