Fastest way to Convert Associative Arrays to Objects

Date Posted: April 23, 2015, 3:28 am


Simply cast an Associative array:

1
2
3
4
5
$obj = (object) array('animal' => 'dog', 'name' => 'simon');

// Then you can access values as objects:
echo $obj->animal; // Outputs dog
echo $obj->name; // Outputs simon

No need to create a class or Function to accomplish it.

About author


Carl Victor Fontanos

A fine gentleman specializing in front-end and back-end development with extensive experience building high performance web applications that keeps users engaged and help businesses grow.

 
Hire Me