1

i have seen a lot of examples but was unsuccessful to help myself. I have a multidimensional array that i want to convert to object format. I tried going levels deeper (multidimensional) but that all. I want to make it an php object. This is my scenario

// this is my array

Array
(
[_cars] => Array
    (
        [_car] => Array
            (
                [0] => Array
                    (
                        [_types] => Array
                            (
                                [_type] => Array
                                    (
                                        [0] => Array
                                            (
                                                [_cc] => 100
                                            )

                                        [1] => Array
                                            (
                                                [_cc] => 100
                                            )

                                        [2] => Array
                                            (
                                                [_cc] => 1000
                                            )

                                    )

                            )
                        ) 
                    )
            )
    ) 
) 

// this is what i want

[_cars] => stdClass Object
    (
        [_car] => Array
            (
                [0] => stdClass Object
                    (
                        [_types] => stdClass Object
                            (
                                [_type] => Array
                                    (
                                        [0] => stdClass Object
                                            (
                                                [_cc] => 999999999999
                                            )

                                        [1] => stdClass Object
                                            (
                                                [_cc] => 999999999999
                                            )

                                        [2] => stdClass Object
                                            (
                                                [_cc] => 999999999999
                                            )

                                    )

                            )
                    )
             )
        )          
ernys
  • 111
  • 1
  • 7
  • 2
    Here's your answer : http://stackoverflow.com/questions/4790453/php-recursive-array-to-object Pretty straightforward ;) – Alex Oct 07 '16 at 09:32

2 Answers2

8

Not sure if it will work but you can try

$object = json_decode(json_encode($array));
Prem Raj
  • 849
  • 4
  • 15
  • Hmm @ÁlvaroGonzález Have you an example of what would make this fail? Not trying to be a pain, I am just interested – RiggsFolly Oct 07 '16 at 09:42
  • @RiggsFolly Sure, here's an [example with a `DateTime` object](https://3v4l.org/Ye52B) that becomes a `stdClass` one. You could argue that's exactly the spec but it can be confusing if you didn't expect it. – Álvaro González Oct 07 '16 at 11:19
  • @ÁlvaroGonzález Thanks for this. Wasn't aware of the DateTime conversion – Prem Raj Oct 10 '16 at 01:54
0

Please check this if this okay

$obj = new stdClass($array); or
$obj = json_decode(json_encode($array));
madankundu
  • 154
  • 8