我被卡住了。我想我需要编写一个递归方法,但我不知道如何......!
我正在尝试将一组父子关系转换为层次树,稍后我将向用户显示该树。
这是我可能拥有的输入数据的示例:
$input = array(
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 2
        ),
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 3
        ),
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 4
        ),
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 212
        ),
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 339
        ),
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 336
        ),
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 6
        ),
    array(
            'itemGroupID' => 1,
            'childItemGroupID' => 5
        ),
    array(
            'itemGroupID' => 6,
            'childItemGroupID' => 8
        ),
    array(
            'itemGroupID' => 6,
            'childItemGroupID' => 9
        ),
    array(
            'itemGroupID' => 6,
            'childItemGroupID' => 10
        ),
    array(
            'itemGroupID' => 6,
            'childItemGroupID' => 11
        ),
    array(
            'itemGroupID' => 6,
            'childItemGroupID' => 12
        ),
    array(
            'itemGroupID' => 6,
            'childItemGroupID' => 13
        ),
    array(
            'itemGroupID' => 6,
            'childItemGroupID' => 74
        ),
    array(
            'itemGroupID' => 9,
            'childItemGroupID' => 15
        ),
    array(
            'itemGroupID' => 10,
            'childItemGroupID' => 16
        ),
    array(
            'itemGroupID' => 11,
            'childItemGroupID' => 17
        ),
    array(
            'itemGroupID' => 12,
            'childItemGroupID' => 18
        ),
    array(
            'itemGroupID' => 13,
            'childItemGroupID' => 19
        ),
    array(
            'itemGroupID' => 74,
            'childItemGroupID' => 75
        )
 );
我希望以如下格式取回数据:
$output = array(
    array(
        'itemGroupID' => 1,
        'children' => array(
              array(
                  'itemGroupID' => 2                   
              ),
              array(
                  'itemGroupID' => 3                   
              ),
              array(
                  'itemGroupID' => 4                   
              ),
              array(
                  'itemGroupID' => 212                   
              ),
              array(
                  'itemGroupID' => 339                   
              ),
              array(
                  'itemGroupID' => 336                  
              ),
              array(
                  'itemGroupID' => 6,
                  'children' => array(
                      array(
                          'itemGroupID' => 8                  
                      ),
                      array(
                          'itemGroupID' => 9,
                          'children' => array(
                              array(
                                  'itemGroupID' => 15
                              )   
                          )                 
                      ), 
                      array(
                          'itemGroupID' => 10,
                          'children' => array(
                              array(
                                  'itemGroupID' => 16
                              )   
                          )                  
                      ), 
                      array(
                          'itemGroupID' => 11,
                          'children' => array(
                              array(
                                  'itemGroupID' => 17
                              )   
                          )                   
                      ), 
                      array(
                          'itemGroupID' => 12,
                          'children' => array(
                              array(
                                  'itemGroupID' => 18
                              )   
                          )                   
                      ), 
                      array(
                          'itemGroupID' => 13,
                          'children' => array(
                              array(
                                  'itemGroupID' => 19
                              )   
                          )                   
                      ), 
                      array(
                          'itemGroupID' => 74,
                          'children' => array(
                              array(
                                  'itemGroupID' => 75
                              )   
                          )                   
                      )                    
                  )               
              ),
              array(
                  'itemGroupID' => 5                   
              )
         )
    )
);
我设法编写了一些代码来告诉我从哪里开始。(因为我担心如果你从第一个节点递归它可能会证明这已经是从链的中途开始了......)
顺便说一句,我输出的元素可用于获取每个层次链的起始索引。
private function _arraySearch($arr, $callback)
{
    foreach ($arr as $key => $item) {
        if ($callback($item)) {
            return $key;
        }
    }
    return false;
}
private function _findRootsOfItemGroupTree($activeItemGroupChildren)
{
    $searchArray = $activeItemGroupChildren;
    $roots = array();
    foreach ($activeItemGroupChildren as $itemGroupChild) {
        $parentItemGroupID = $itemGroupChild['itemGroupID'];
        $found = array_filter($searchArray, function ($element) use ($parentItemGroupID) {
            return $element['childItemGroupID'] == $parentItemGroupID;
        });
        $rootItemGroupID = $parentItemGroupID;
        if (count($found) == 0
            && $this->_arraySearch($roots,
                function ($element) use ($rootItemGroupID) {
                    return $element['itemGroupID'] == $rootItemGroupID;
                }) === false) {
            $roots[] = $itemGroupChild;
        }
    }
    return $roots;
}
但是,我现在需要使用这些信息来创建一个新的关联数组。我不知道怎么做。(我会在几分钟内发布一些作品..)
想法?
注意:假设此结构中没有递归循环,即使它们在技术上可能存在。