鏈嬪弸缁欐垜鍑虹殑涓閬撻锛屽垹闄ら噷闈㈠惈鏈塱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 [];
}
}
鍙戣〃鍥炲