在PHP中从嵌套JSON对象中查找键值

时间:2017-06-22 10:47:28

标签: php json

我试图在值匹配时删除json对象。我尝试了很多方法,但却遇到了基于children的对象。以下是我的json的样子:

[{
        "id": "gx14jg",
        "split": "",
        "slug": "f4-1",
        "url": "",
        "name": "f4",
        "type": "other_type_first-category",
        "class": "undefined"
    }, {
        "id": "bo3m0q",
        "split": 2,
        "slug": "\/",
        "url": "",
        "name": "home",
        "type": "page",
        "class": ""
    },
    [{
        "id": "g3qjsv",
        "split": "",
        "slug": "demo",
        "url": "",
        "name": "demo",
        "type": "page",
        "class": "",
        "children": [{
            "id": "r318jh",
            "url": "",
            "slug": "f4-1",
            "name": "f4",
            "type": "other_type_first-category",
            "class": "undefined",
            "children": [{
                "id": "hztbje",
                "url": "",
                "slug": "f3",
                "name": "f3",
                "type": "other_type_first-category",
                "class": "undefined"
            }]
        }]
    }],
    [{
        "id": "r9s608",
        "split": "",
        "slug": "demo2",
        "url": "",
        "name": "demo2",
        "type": "category",
        "class": "undefined",
        "children": [{
            "id": "dlk12g",
            "url": "",
            "slug": "asddddddddd",
            "name": "asddddddddd",
            "type": "category",
            "class": "undefined"
        }]
    }],
    [{
        "id": "qg6c3p",
        "split": 2,
        "slug": "motorola",
        "url": "",
        "name": "Motorola",
        "type": "product-category",
        "class": "moto",
        "children": [{
            "id": "rem23f",
            "url": "",
            "slug": "indian-resturants",
            "name": "Indian Resturants",
            "type": "Service-category",
            "class": "undefined",
            "children": [{
                "id": "kpwiq0",
                "url": "",
                "slug": "f4-1",
                "name": "f4",
                "type": "other_type_first-category",
                "class": "undefined"
            }]
        }]
    }]
]

我必须找到一个" slug"有价值" f3"并删除SLUG保存f3的同一对象

以下是我迄今为止的尝试方式。 $ nav是json数组,$slugf3

public function checkCategorySlugInNavigation($nav, $slug){

        $ArrayRemoved = [];

        if(is_array($nav)){
            foreach($nav as $key=>$navVal){


                if($navVal->slug == $slug){
                    unset($nav[$key]);
                }
                else{

                    if(array_key_exists('children', $navVal)){

                        $navVal = $this->checkCategorySlugInNavigation($navVal, $slug);
                    }
                    $ArrayRemoved[] = $navVal;
                }
            }
        } else{

            if($nav->slug == $slug){
                unset($nav[$key]);
            } else{
                if(array_key_exists('children', $nav)){

                    $nav = $this->checkCategorySlugInNavigation($nav->children, $slug);
                }
                $ArrayRemoved[] = $nav;
            }
        }

        return $ArrayRemoved;
    }

任何支持性的答案都会对此有所帮助。

预期结果将是:

[{
        "id": "gx14jg",
        "split": "",
        "slug": "f4-1",
        "url": "",
        "name": "f4",
        "type": "other_type_first-category",
        "class": "undefined"
    }, {
        "id": "bo3m0q",
        "split": 2,
        "slug": "\/",
        "url": "",
        "name": "home",
        "type": "page",
        "class": ""
    },
    [{
        "id": "g3qjsv",
        "split": "",
        "slug": "demo",
        "url": "",
        "name": "demo",
        "type": "page",
        "class": "",
        "children": [{
            "id": "r318jh",
            "url": "",
            "slug": "f4-1",
            "name": "f4",
            "type": "other_type_first-category",
            "class": "undefined"
        }]
    }],
    [{
        "id": "r9s608",
        "split": "",
        "slug": "demo2",
        "url": "",
        "name": "demo2",
        "type": "category",
        "class": "undefined",
        "children": [{
            "id": "dlk12g",
            "url": "",
            "slug": "asddddddddd",
            "name": "asddddddddd",
            "type": "category",
            "class": "undefined"
        }]
    }],
    [{
        "id": "qg6c3p",
        "split": 2,
        "slug": "motorola",
        "url": "",
        "name": "Motorola",
        "type": "product-category",
        "class": "moto",
        "children": [{
            "id": "rem23f",
            "url": "",
            "slug": "indian-resturants",
            "name": "Indian Resturants",
            "type": "Service-category",
            "class": "undefined",
            "children": [{
                "id": "kpwiq0",
                "url": "",
                "slug": "f4-1",
                "name": "f4",
                "type": "other_type_first-category",
                "class": "undefined"
            }]
        }]
    }]
]

谢谢(提前)!

2 个答案:

答案 0 :(得分:0)

我已经对你的功能做了一些改变。我改用了refrence。检查此解决方案

public function checkCategorySlugInNavigation(&$nav, $slug){ //<---pass by reference so unset will effect on real result

        $ArrayRemoved = [];

        if(is_array($nav)){
            foreach($nav as $key=>$navVal){              

                if(isset($navVal->slug) && $navVal->slug == $slug){
                    unset($nav[$key]);                    
                }
                else{

                    if(is_array($navVal)) //<--- In your example children exist in array so need to check it also
                        $navVal = (isset($navVal[0]))?$navVal[0]:$navVal;
                    if(array_key_exists('children', $navVal)){
                        $navVal = $this->checkCategorySlugInNavigation($navVal, $slug);
                    }

                }
            }
             $ArrayRemoved[] = $nav; //<---- add array at end
        } else{            
        if($nav->slug == $slug){                
            unset($nav[$key]);
        } else{
            if(array_key_exists('children', $nav)){    
                $nav_tmp = $this->checkCategorySlugInNavigation($nav->children, $slug); //<-------update this
                if(count($nav_tmp) == 0)
                    unset($nav->children); //<---- unset value
            }                
        }
    }

        return $ArrayRemoved;
    }
}

答案 1 :(得分:0)

试试吧。简单而实用。

function remove_f3_children( $array ) {

    foreach( $array as $key => &$value ) {

        if( is_array( $value ) && isset( $value[0]->children ) ) {
            $val = remove_f3_children( $value[0] );

            if( $val->children === null )
                unset( $value[0]->children );
            else
                $value = $val;
        }

        if( is_array( $value ) && ! isset( $value[0]->children ) ) {
            if( array_search( 'f3', (array) $value[0] ) == 'slug' )
                $value = null;
        }

    }

    return $array;
}

print_r( remove_f3_children( json_decode( $json ) ) );

只需更改功能名称!