将多维数组转换为php

时间:2016-10-07 09:31:08

标签: php arrays object multidimensional-array

我见过很多例子但是没有成功帮助自己。我有一个多维数组,我想转换为对象格式。我尝试了更深层次(多维),但所有。我想把它变成一个php对象。这是我的方案

// 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
                                            )

                                    )

                            )
                    )
             )
        )          

2 个答案:

答案 0 :(得分:4)

不确定它是否可行,但您可以尝试

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

答案 1 :(得分:0)

如果没问题,请检查一下

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