Wednesday, 29 August 2018

How to convert a multidimensional array into an object in php?

I have a multidimensional array:

$image_path = array('sm'=>$sm,'lg'=>$lg,'secondary'=>$sec_image);

witch looks like this:
[_media_path:protected] => Array
            (
                [main_thumb] => http://example.com/e4150.jpg
                [main_large] => http://example.com/e4150.jpg
                [secondary] => Array
                    (
                        [0] => http://example.com/e4150.jpg
                        [1] => http://example.com/e4150.jpg
                        [2] => http://example.com/e9243.jpg
                        [3] => http://example.com/e9244.jpg
                    )

            )

and i would like to convert it into an object and retain the key names.
Any ideas?
Thanks
edit: $obj = (object)$image_path; doesn't seem to work. i need a different way of looping through the array and creating a object

As simple as
$obj = (object)$image_path;

For reference, this is documented here.

0 comments:

Post a Comment