"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;
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;
simple yet effective.
Should be noted this DOES NOT work with Multi dimensional array
Have a fresh tip? Share with Coderwall community!
PHP applications still send a browser full of JavaScript. Hardening the server does not inventory the payment, analytics, and plugin scripts that run after the HTML is delivered.
Security insights from Cside