"One line" PHP Array to Object
As I can't comment on @buckheroux pro tip, it's always nice to remeber that you can convert an array to an object just typecasting the variable:
$obj = (object) $array;
Clean and straight to the point :)
And the manual for other references:
http://www.php.net/manual/en/language.types.object.php#language.types.object.casting
Written by Leonardo Prado
Related protips
4 Responses
Casting can also convert an object to array
$array = (array) $obj;
over 1 year ago
·
I do this every time I want to create a stdClass out of an array. Except when the array is empty by default, then there's no real time saver over new stdClass;
over 1 year ago
·
simple yet effective.
over 1 year ago
·
Should be noted this DOES NOT work with Multi dimensional array
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#