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
                                            )

                                    )

                            )
                    )
             )
        )          
1

2 Answers 2

8

Not sure if it will work but you can try

$object = json_decode(json_encode($array));
Sign up to request clarification or add additional context in comments.

3 Comments

Hmm @ÁlvaroGonzález Have you an example of what would make this fail? Not trying to be a pain, I am just interested
@RiggsFolly Sure, here's an example with a DateTime object that becomes a stdClass one. You could argue that's exactly the spec but it can be confusing if you didn't expect it.
@ÁlvaroGonzález Thanks for this. Wasn't aware of the DateTime conversion
0

Please check this if this okay

$obj = new stdClass($array); or
$obj = json_decode(json_encode($array));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.