"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
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/40842/f02730058b5b48b4ab84a0314c252adb.jpeg)
Casting can also convert an object to array
$array = (array) $obj;
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/43547/87f227b9aa2967224be3dde79eb0163d.jpeg)
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
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/1121/9453c6dc5454a47511d40c9a7f763455.jpg)
simple yet effective.
over 1 year ago
·
![](https://coderwall-assets-0.s3.amazonaws.com/uploads/user/avatar/13271/8.jpg)
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#