Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Function_Multidimensional Array - Fatal编程技术网

层次结构数组中的php移位

层次结构数组中的php移位,php,arrays,function,multidimensional-array,Php,Arrays,Function,Multidimensional Array,我有下一个数组: Array( [id] => 1 [children] => Array( [2] => Array( [id] => 2 [inactive] => true [children] => Array( [4] => Array( [id] => 4 [children]

我有下一个数组:

Array(
   [id] => 1
   [children] => Array(
      [2] => Array(
         [id] => 2
         [inactive] => true
         [children] => Array(
            [4] => Array(
               [id] => 4
               [children] => Array()
            )
         )
      )
      [3] => array(
         [id] => 3
         [children] => Array(
            [5] => Array(
               [id] => 5
               [inactive] => true
               [children] => Array()
            )
         )
      )
   )
)
我需要从这个数组中删除元素,这些元素的[inactive]=true。但是我的问题在下一个。我应该移动数组元素。 输出应为:

Array(
   [id] => 1
   [children] => Array(
      [4] => Array(
         [id] => 4
         [children] => Array()
      )
      [3] => array(
         [id] => 3
         [children] => Array(
         )
      )
   )
)
这是我的职责。但它删除了数组元素及其所有子元素

public function deleteInactive($userTree)
{
    if (!empty($userTree)) {
        foreach($userTree['children'] as $userId => &$user) {
            if (array_key_exists('inactive', $user)) {
                $userTree['children'] += $user['children'];
                unset($userTree['children'][$userId]);
                $this->deleteInactive($userTree);
                break;
            }
            $this->deleteInactive($user);
        }
    }
    return $userTree;
}
你能帮我修改这个函数吗


非常感谢。

在取消设置节点之前,需要将子节点附加到节点的父节点。这不会发生(代码只会取消设置),因此子项丢失。

尝试此功能,它应该按照您的要求执行

<?php
function deleteInactive($children) {
    $copy = $children;
    foreach ($copy as $key => $child) {
        if (!empty($child['inactive']) && $child['inactive'] === true) {
            unset($children[$key]);
            $children = deleteInactive($child['children']);
        } elseif (!empty($child['children']) && is_array($child['children'])) {
            $children[$key]['children'] = deleteInactive($child['children']);
        }
    }
    return $children;
} ?>

谢谢查看我编辑的函数。但这是行不通的。它是删除第一个元素,然后附加子元素。但它只使用第一个非活动元素。
deleteInactive(array('1' => $array));