I am looping over an array of groups, each element containing a parent 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
}
}
This works fine on a array like this:
group
-group
--group
---group
----group
----- group <- the only group on the 5th layer
but not with an array which has multiple groups on the 5th level:
group
-group
--group
--group
---group
----group
-----group <- 5th layer
----group
-----group <- 5th layer too, but diff parent
How can I resolve this to get the special function called, even when there are multiple groups with multiple parents on the fifth level in the array?
I hope that I formulated my question clear enough.
Try iterating trough the the groups always searching for groups whose level can be determinated by its parent level. Something like this: