閫掑綊鍒犻櫎

鏈嬪弸缁欐垜鍑虹殑涓閬撻锛屽垹闄ら噷闈㈠惈鏈塱s_deleted鐨勯敭鍊煎

{
    "name": "骞夸笢姹曞ご鍗庝鲸涓",
    "is_deleted": 0,
    "children": [
        {
            "name": "瀹為獙妤",
            "is_deleted": 0,
            "children": [
                {
                    "name": "瀹為獙妤1",
                    "is_deleted": 1,
                    "children": []
                },
                {
                    "name": "榛勯工妤",
                    "is_deleted": 0,
                    "children": [
                        {
                            "name": "瀹為獙妤1",
                            "is_deleted": 0,
                            "children": [
                                {
                                    "name": "瀹為獙妤11",
                                    "is_deleted": 0,
                                    "children": []
                                },
                                {
                                    "name": "瀹為獙妤22",
                                    "is_deleted": 1,
                                    "children": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

鏈潵浠ヤ负鏄竴涓尯瀹规槗鐨勯鐩紝鍐欒捣鏉ヨ繕鏄病閭d箞绠鍗曪紝浣嗘槸涔熸尯鏈夋敹鑾蜂簡銆

<?php
$map = ' {
    "name": "骞夸笢姹曞ご鍗庝鲸涓",
    "is_deleted": 0,
    "children": [
        {
            "name": "瀹為獙妤",
            "is_deleted": 0,
            "children": [
                {
                    "name": "瀹為獙妤1",
                    "is_deleted": 1,
                    "children": []
                },
                {
                    "name": "榛勯工妤",
                    "is_deleted": 0,
                    "children": [
                        {
                            "name": "瀹為獙妤1",
                            "is_deleted": 0,
                            "children": [
                                {
                                    "name": "瀹為獙妤11",
                                    "is_deleted": 0,
                                    "children": []
                                },
                                {
                                    "name": "瀹為獙妤22",
                                    "is_deleted": 1,
                                    "children": []
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}

    ';
    $map = json_decode($map, true);
    if($map['is_deleted'] == 1){
        $map = [];
    }
    if(!empty($map["children"])){
        $map["children"] = del_deleted_arr($map["children"]);
    }
    echo json_encode($map);exit;


    function del_deleted_arr($arr){
        // echo json_encode($arr);exit;
        if($arr){
            for($key=0; $key<count($arr);$key++){
            //foreach ($arr as $key => $value) {
                if($arr[$key]['is_deleted'] == 1){
                     array_splice($arr, $key, 1);
                     $key = $key -1;
                } 
                $arr[$key]['children'] = del_deleted_arr($arr[$key]['children']);
            }
            //$arr = array_values($arr);
            return  $arr;
        }else{
            return [];
        }
    }

All posts

Other pages

鍙戣〃璇勮

鐢靛瓙閭欢鍦板潃涓嶄細琚叕寮銆 蹇呭~椤瑰凡鐢*鏍囨敞