1

我正在循环一组组,每个元素都包含一个父 ID。

$currentparent = $group['grpId']; //$group is the current element in a loop wrapped around this piece of code
$currentlevel = 0;

foreach($groups as $grp)
{
    $parent = $grp['grpParentId'];

    if($parent != $currentparent && $currentlevel != 6)
    {
        //adding layer
        $currentlevel++;
        //changing parent
        $currentparent = $grp['grpParentId'];
    }

    if($currentlevel == 6)
    {
        //call a special function
    }
    else
    {
        //call the regular function
    }
}

这适用于这样的数组:

group
-group
--group
---group
----group
----- group <- the only group on the 5th layer

但不适用于在第 5 级具有多个组的数组:

group
-group
--group
--group
---group
----group
-----group <- 5th layer
----group
-----group <- 5th layer too, but diff parent

即使在数组的第五级有多个具有多个父级的组时,我如何解决此问题以调用特殊函数?

我希望我的问题足够清楚。

4

2 回答 2

1

尝试遍历组,始终搜索其级别可以由其父级别确定的组。像这样的东西:

// example input
$groups = array(
    array('grpParentId' => 0, 'grpId' => 1, ),
    array('grpParentId' => 1, 'grpId' => 2, ),
    array('grpParentId' => 2, 'grpId' => 3, ),
    array('grpParentId' => 3, 'grpId' => 4, ),
    array('grpParentId' => 4, 'grpId' => 5, ),
    array('grpParentId' => 5, 'grpId' => 6, ),
    array('grpParentId' => 6, 'grpId' => 7, ),

    array('grpParentId' => 5, 'grpId' => 8, ),
    array('grpParentId' => 8, 'grpId' => 9, ),
);


shuffle($groups); // just for testing the logic does in fact tolerate randomly ordered input

$rootId = 1; // set to the rootnode's id
$grouplevels = array();

// find the rootnode first
foreach($groups as $i => $grp) {
    if ($rootId == $grp['grpId']) {
        $grouplevels[$rootId] = 1;
        unset($groups[$i]);
        break;
    }
}

// figure out childgroup levels
do {
    $old_count = count($groups);
    foreach($groups as $i => $grp) {
        if (in_array($grp['grpParentId'], array_keys($grouplevels))) {
            // the current node's parent's level was determinated previously, we can tell this group's level as well
            $grouplevels[$grp['grpId']] = $level = $grouplevels[$grp['grpParentId']]+1;

            if ($level == 6) {
                print $grp['grpId']."\n";
            }

            // remove from the "we dont know yet" list
            unset($groups[$i]);
        }
    }
} while (count($groups) < $old_count); // run while we can sort out at least one group's level in the current iteration

// handle the case when not every group's level could been determinated!
if (!empty($groups)) {
    print "inconsitency ahead!";
}
于 2012-11-13T16:38:24.387 回答
0

它可以通过使用递归模式来解决。下面的代码应该可以解决问题。它可能可以优化。

$currentparent = $group['grpId']; //$group is the current element in a loop wrapped around         
$this piece of code
$currentlevel = 0;

// Initialize 
foreach($groups as $grp)
{
    $grp['children'] = array();

}

foreach($groups as $grp)
{
    $parent = $grp['grpParentId'];
    $parent['children'][] = $grp;  
}

foreach($groups as $grp)
{
    if(empty($parent['children'])) $root = $grp; // Alternatively check if parent is null or something. 
}


function recursive_count($root, $lvl = 0)
{
    if($currentlevel == 6)
    {
        //call a special function
    }
    else
    {
        //call the regular function
    }
    foreach($root['children'] as $children)
    {
       recursive_count($children, $lvl++);
    }


 }

 recursive_count($root);

更新:如果内存消耗是一个问题,可以在将组添加到子数组时使用引用。

更新二:即使算法有 4 个 foreach 和递归结构,运行时间仍然是 O(n),其中 n 是图的大小。

于 2012-11-13T16:36:11.477 回答