I wrote a quick and simple function which allows you to quickly convert arrays to objects. It allows you to change an array like this :
$a[‘b’];
To an object like this :
$a -> b;
Below is the function. Have fun!
Tue, May 6, 2008
I wrote a quick and simple function which allows you to quickly convert arrays to objects. It allows you to change an array like this :
To an object like this :
Below is the function. Have fun!
July 23rd, 2008 at 3:14 pm
function array_to_object($array = array()) {
return (object) $array;
}
July 23rd, 2008 at 3:42 pm
@patrick
Thank you for the type casting example. It is useful!
December 2nd, 2008 at 12:35 am
This will not work with multi dimensional arrays.
December 2nd, 2008 at 1:12 am
@Hari
You are right, it will not work with multi-dimensional arrays.
You could create a recursive type of function with a break to handle that though.
Below is an example of how the array is converted to an object.
http://lost-in-code.com/wp-content/projects/php-array-to-object/
The example I wrote above uses type casting.
Either way, it only converts the first array instance and not the inner arrays.
Best,
Antonie