PHP在阵列树中反转父项和子项

时间:2017-04-19 11:12:28

标签: php arrays tree

我有一个阵列/树(无尽的)"父母"而不是孩子。它看起来像这样:

array(5) {
  [0]=>
  array(3) {
    ["group_id"]=>
    int(1)
    ["name"]=>
    string(11) "Toplevel"
    ["parents"]=>
    NULL
  }
  [1]=>
  array(3) {
    ["group_id"]=>
    int(2)
    ["name"]=>
    string(13) "SecondLevel"
    ["parents"]=>
    array(1) {
      [0]=>
      array(3) {
        ["group_id"]=>
        int(1)
        ["name"]=>
        string(11) "Toplevel"
        ["parents"]=>
        NULL
      }
    }
  }
  [2]=>
  array(3) {
    ["group_id"]=>
    int(4)
    ["name"]=>
    string(12) "ThirdLevel"
    ["parents"]=>
    array(2) {
      [0]=>
      array(3) {
        ["group_id"]=>
        int(2)
        ["name"]=>
        string(13) "SecondLevel"
        ["parents"]=>
        array(1) {
          [0]=>
          array(3) {
            ["group_id"]=>
            int(1)
            ["name"]=>
            string(11) "TopLevel"
            ["parents"]=>
            NULL
          }
        }
      }
    }
  }

如您所见,树结构的顺序相反。我想以正确的顺序重建树,子节点作为子节点。它应该是这样的:

array(5) {
  [0]=>
  array(3) {
    ["group_id"]=>
    int(1)
    ["name"]=>
    string(11) "Toplevel"
    ["children"]=>
    NULL
  }
  [1]=>
  array(3) {
    ["group_id"]=>
    int(1)
    ["name"]=>
    string(13) "TopLevel"
    ["children"]=>
    array(1) {
      [0]=>
      array(3) {
        ["group_id"]=>
        int(2)
        ["name"]=>
        string(11) "SecondLevel"
        ["children"]=>
        NULL
      }
    }
  }
  [2]=>
  array(3) {
    ["group_id"]=>
    int(1)
    ["name"]=>
    string(12) "TopLevel"
    ["children"]=>
    array(2) {
      [0]=>
      array(3) {
        ["group_id"]=>
        int(2)
        ["name"]=>
        string(13) "SecondLevel"
        ["children"]=>
        array(1) {
          [0]=>
          array(3) {
            ["group_id"]=>
            int(4)
            ["name"]=>
            string(11) "ThirdLevel"
            ["children"]=>
            NULL
          }
        }
      }
    }
  }

我只是不知道该怎么做。是否有任何我可以使用的简单功能,例如uasort?

更新以回答问题:

由于这个原因,数组就是这样构建的:

  • 用户A可以是不同群组的成员
  • 群组可以是不同子群组的成员
  • 子组可以是不同子组的成员(依此类推)

现在用户A在第1组和第6组中.6是5的子组,它是4的子组,是3的子组。这意味着,用户A也是组的成员5,4 ,3。为了获取与用户A相关联的每个组,我将查找他是(1和6)的直接成员的每个组并查找这些组的所有父组(结果:1,3,4,5, 6)具有递归函数:

private function getParentsArray($group_id) {
    $ret=null;
    $parents = $this->getGroupParents($group_id);

    if(!is_null($parents[0])) {
        foreach($parents as $parent) {
            $group = $this->getGroup($parent);
            if(!empty($this->getGroupParents($parent)) && $this->getGroupParents($parent)[0]) {
                $ret[] = array("group_id" => $group["group_id"], "name" => $group["name"], "parents" => $this->getParentsArray($group["group_id"]));
            } else {
                $ret[] = array("group_id" => $group["group_id"], "name" => $group["name"], "parents" => null);
            }
        }
    }
    return $ret;
}

更新2: 这是现有数组的var_export,按要求:

array (
  0 => 
  array (
    'group_id' => 1,
    'name' => 'Toplevel',
    'parents' => NULL,
  ),
  1 => 
  array (
    'group_id' => 2,
    'name' => 'Secondlevel',
    'parents' => 
    array (
      0 => 
      array (
        'group_id' => 1,
        'name' => 'Toplevel',
        'parents' => NULL,
      ),
    ),
  ),
  2 => 
  array (
    'group_id' => 4,
    'name' => 'Thirdlevel',
    'parents' => 
    array (
      0 => 
      array (
        'group_id' => 2,
        'name' => 'Secondlevel',
        'parents' => 
        array (
          0 => 
          array (
            'group_id' => 1,
            'name' => 'Toplevel',
            'parents' => NULL,
          ),
        ),
      ),
      1 => 
      array (
        'group_id' => 3,
        'name' => 'Secondlevel2',
        'parents' => 
        array (
          0 => 
          array (
            'group_id' => 1,
            'name' => 'Toplevel',
            'parents' => NULL,
          ),
        ),
      ),
    ),
  ),
  3 => 
  array (
    'group_id' => 6,
    'name' => 'Firstlevel',
    'parents' => NULL,
  ),
  4 => 
  array (
    'group_id' => 10,
    'name' => 'Forthlevel',
    'parents' => 
    array (
      0 => 
      array (
        'group_id' => 9,
        'name' => 'Thirdlevel2',
        'parents' => 
        array (
          0 => 
          array (
            'group_id' => 7,
            'name' => 'Secondlevel3',
            'parents' => 
            array (
              0 => 
              array (
                'group_id' => 6,
                'name' => 'Firstlevel',
                'parents' => NULL,
              ),
            ),
          ),
        ),
      ),
    ),
  ),
)

1 个答案:

答案 0 :(得分:0)

  1. 计算父母总数

    $ parents = $ this-> getGroupParents($ group_id);

  2. 在for语句中,将数组($ ret [])反向存储

    为($ I ==计数($父母); $ I -1个; $ I - ){      $ ret [$ i] ='&#39 ;; }

  3. 使用key parent => children