How to convert a multidimensional array to an object in PHP

Today I was working on creating an object from an array of data, I didn’t want a full blown class as I was only accessing the data as application settings  and thankfully due to PHP’s stdClass class this can be easily achieved like so:-

$obj = new stdClass($array);

or like…

$obj = (object) $array

However, problems occur as I found out today that when trying to create an object from a multidimensional array only the top level of the array is converted to an object but sub-array are not.

To solve this problem you can do the following:-

$obj = json_decode(json_encode($array));

I thought I’d just post this up as a very simple way to convert a multidimensional array to an object.