Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:36:30+00:00 2026-06-14T07:36:30+00:00

I am looping over an array of groups, each element containing a parent id.

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:36:32+00:00Added an answer on June 14, 2026 at 7:36 am

    Try iterating trough the the groups always searching for groups whose level can be determinated by its parent level. Something like this:

    // 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!";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have HTML table. I'm looping through table and iterate over each row whose
What I am doing is looping over an array and running a function on
I am working on appending a file extension while looping over an array. The
I'm looping over an array named shopData and need to use values within that
I'm looping over an array of JSON data returned from couchdb, and added the
i have the following code which is meant to be looping over an array,
I have a JSON object that I am looping over with each() to add
if i'm looping over an array, and while in the middle of one of
I'm trying to read a file of regexes, looping over them and filtering them
Why does using a primitive data type work in the second for-each loop when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.