Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 无法按键从数组中删除或取消设置特定项_Php_Arrays_Foreach - Fatal编程技术网

Php 无法按键从数组中删除或取消设置特定项

Php 无法按键从数组中删除或取消设置特定项,php,arrays,foreach,Php,Arrays,Foreach,下面是我的数组输出 Array ( [0] => Array ( [id] => 1011 [user_id] => 168 [item_id] => 831 [post_content] => My New Post 20 [parent_comment_id] => 1010 [name]

下面是我的数组输出

Array
(
    [0] => Array
        (
            [id] => 1011
            [user_id] => 168
            [item_id] => 831
            [post_content] => My New Post 20
            [parent_comment_id] => 1010
            [name] => a
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 1012
                            [user_id] => 168
                            [item_id] => 831
                            [parent_comment_id] => 1011
                            [name] => a
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 1013
                                            [user_id] => 179
                                            [item_id] => 831
                                            [parent_comment_id] => 1012
                                            [name] => a
                                            [children] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [id] => 1014
                                                            [user_id] => 168
                                                            [item_id] => 831
                                                            [parent_comment_id] => 1013
                                                            [name] => a
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)
我只想从子数组中删除以下数组键

我已经尝试过删除,但它也从起始数组中删除。我只想从子数组中删除该键

下面是我的代码

$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){

    $tree = array();

    foreach ($parent as $k=>$l){


        if(isset($list[$l['id']])){
            $l['children'] = createTree($list, $list[$l['id']]);
            //unset($l['item_id']);
        }
        $tree[] = $l;

    } 
    return $tree;
}

这只需要对递归函数做一点小小的修改。我添加了一个新参数,用于标记这是否是树的基础。在代码将新节点添加到树中之前,如果它不是基节点,它将首先删除所需的元素

后续调用全部传入false,以标记它不是基本调用

$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;
}

您可以在子项中使用删除标志,如下所示:

function createTree(&$list, $parent, $base = true){
    $tree = array();
    foreach ($parent as $k=>$l){
        if(isset($list[$l['id']])){
            $l['children'] = createTree($list, $list[$l['id']], false);
        }
        if ( ! $base )  {
            unset($l['item_id']);
            unset($l['parent_comment_id']);
            unset($l['name']);
        }
        $tree[] = $l;
   }
    return $tree;
}
$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;
}