lnfyqw
Last Updated: February 25, 2016
·
2.934K
· leonardodna
44c5fbea96c3b8c76a0ae4b266f058c9

"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

Say Thanks
Respond

4 Responses
Add your response

1074
F02730058b5b48b4ab84a0314c252adb

Casting can also convert an object to array
$array = (array) $obj;

over 1 year ago ·
1275
87f227b9aa2967224be3dde79eb0163d

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 ·
3891
9453c6dc5454a47511d40c9a7f763455

simple yet effective.

over 1 year ago ·
5394
8

Should be noted this DOES NOT work with Multi dimensional array

over 1 year ago ·