蝴蝶不菲
您可以在子项中使用标志进行删除,如下所示:$arr = array( array('id'=>1011, 'user_id' => 168, 'item_id'=>831, 'post_content'=>'My New Post 20', 'parent_comment_id'=>1010, 'name'=>'a'), array('id'=>1012,'user_id' => 168, 'item_id'=>831 ,'parent_comment_id'=>1011, 'name'=>'a'), array('id'=>1013, 'user_id' => 179, 'item_id'=>831, 'parent_comment_id'=>1012, 'name'=>'a'), array('id'=>1014,'user_id' => 168, 'item_id'=>831, 'parent_comment_id'=>1013, 'name'=>'a'),);echo "<pre> add";print_r($arr);$new = array();foreach ($arr as $a){ $new[$a['parent_comment_id']][] = $a;}$tree = createTree($new, array($arr[0]));print_r($tree);function createTree(&$list, $parent, $isChild = false){ $tree = array(); foreach ($parent as $k=>$l){ if(isset($list[$l['id']])){ $l['children'] = createTree($list, $list[$l['id']], true); if ($isChild) { unset($l['item_id']); // unset another field } } $tree[] = $l; } return $tree;}