PHP如何递归将对象转化成数组,并且递归去除键名
2018-01-25| 程成| 1097| 0| PHP技术

在PHP当你获得一个多维对象时(你也不知道它最多有几维),你需要将它转换成数组,有时候你还需要将它前面 "key" 去除。例子如下:


{
    "status":1,
    "data":{
        "104":{
            "id":"104",
            "parent_id":"0",
            "value":"CPLB000047",
            "label":"所有类别",
            "children":{
                "105":Object{...},
                "106":{
                    "id":"106",
                    "parent_id":"104",
                    "value":"CPLB000048",
                    "label":"工业继电器",
                    "children":{
                        "107":{
                            "id":"107",
                            "parent_id":"106",
                            "value":"CPLB000049",
                            "label":"常规继电器",
                            "children":{
                                "112":{
                                    "id":"112",
                                    "parent_id":"107",
                                    "value":"CPLB000054",
                                    "label":"小型电磁式继电器",
                                    "children":[
                                    ]
                                },
                                "113":{
                                    "id":"113",
                                    "parent_id":"107",
                                    "value":"CPLB000055",
                                    "label":"接触器式继电器",
                                    "children":[
                                    ]
                                },
                                "114":{
                                    "id":"114",
                                    "parent_id":"107",
                                    "value":"CPLB000056",
                                    "label":"接口继电器",
                                    "children":[
                                    ]
                                },
                                "115":{
                                    "id":"115",
                                    "parent_id":"107",
                                    "value":"CPLB000057",
                                    "label":"固态继电器",
                                    "children":[
                                    ]
                                },
                                "116":{
                                    "id":"116",
                                    "parent_id":"107",
                                    "value":"CPLB000058",
                                    "label":"印刷基板用继电器",
                                    "children":[
                                    ]
                                }
                            }
                        },
                        "108":Object{...},
                        "109":Object{...},
                        "110":Object{...},
                        "111":Object{...}
                    }
                }
            }
        }
    },
    "message":"获取成功"
}


转换成这样子:


{
    "status":1,
    "data":[
        {
            "id":"104",
            "parent_id":"0",
            "value":"CPLB000047",
            "label":"所有类别",
            "children":[
                Object{...},
                {
                    "id":"106",
                    "parent_id":"104",
                    "value":"CPLB000048",
                    "label":"工业继电器",
                    "children":[
                        {
                            "id":"107",
                            "parent_id":"106",
                            "value":"CPLB000049",
                            "label":"常规继电器",
                            "children":[
                                {
                                    "id":"112",
                                    "parent_id":"107",
                                    "value":"CPLB000054",
                                    "label":"小型电磁式继电器",
                                    "children":[
                                    ]
                                },
                                {
                                    "id":"113",
                                    "parent_id":"107",
                                    "value":"CPLB000055",
                                    "label":"接触器式继电器",
                                    "children":[
                                    ]
                                },
                                {
                                    "id":"114",
                                    "parent_id":"107",
                                    "value":"CPLB000056",
                                    "label":"接口继电器",
                                    "children":[
                                    ]
                                },
                                {
                                    "id":"115",
                                    "parent_id":"107",
                                    "value":"CPLB000057",
                                    "label":"固态继电器",
                                    "children":[
                                    ]
                                },
                                {
                                    "id":"116",
                                    "parent_id":"107",
                                    "value":"CPLB000058",
                                    "label":"印刷基板用继电器",
                                    "children":[
                                    ]
                                }
                            ]
                        },
                        Object{...},
                        Object{...},
                        Object{...},
                        Object{...}
                    ]
                }
            ]
        }
    ],
    "message":"获取成功"
}



你需要将前面的 "key",比如 104,105,106...,同时将对象都替换成了数组。你可以使用下面这个PHP方法,实现递归将对象转化成数组,并且递归去除键名:


/**
     * Function: 递归将对象转换成数组
     * User: chengcheng
     * Date: 2018/1/23 16:08
     * Return: 对象替换成数组
     */
    public function objectToArray($object){
        $array = array_values($object);
        foreach($array as $k=>$v){
            if( gettype($v['child'])=='object' || gettype($v['child'])=='array' ){
                $array[$k]['child']=array_values(self::objectToArray($v['child']));
            }
        }
        return $array;
    }


注:这里我还递归替换了键名,你可以看这里 PHP中递归替换多维数组键名和键值方法



×
作者:程成
QQ:492245711