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

  • Home
  • SEARCH
  • 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 470791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:55:13+00:00 2026-05-12T23:55:13+00:00

private function find_children ($parent_id, $children, &$result) { foreach ($children as $c) { if ($c->parent_comment_id

  • 0
private function find_children ($parent_id, $children, &$result)
{              
    foreach ($children as $c)
    {            
        if ($c->parent_comment_id == $parent_id)
        {                
            $result[] = $c;
            $this->find_children($c->id, $children, $result);            
        }            
    }
    return;        
}

The above function is supposed to take a starting parent id and recursively go through an array of children nodes (really just an object with a unique id and a parent id) sorting them so that each node comes directly after it’s parent (see below for sample data).

But for some reason, the function doesn’t execute as I expect. I have the following data for testing.

id: 1 pid: 0 (the initial parent which is not in the children array passed to func. problem?)
id: 2 pid: 1
id: 3 pid: 2
id: 4 pid: 1
id: 5 pid: 3
id: 6 pid: 5
id: 7 pid: 4
id: 8 pid: 3

and want the following array returned:
1, 4, 7, 2, 3, 8, 5, 6

But instead, I get:
1, 2, 3, 5, 6

which, while they’re in the right order, a few are missing.

I have not had the need to do recursion in many years, so likely I am missing something obvious, although not so obvious to myself.

In case anyone is wondering, or if it matters, I’m trying to build a q&a commenting system where every post can have multiple replies.

Thus:

initial post 
-reply to initial post #1
--reply to reply
-reply to initial post #2
-- reply to above
--- reply to above
--reply to #2
  • 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-05-12T23:55:13+00:00Added an answer on May 12, 2026 at 11:55 pm

    I think you’re looking for something like a Nested Set Tree. It should do what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function like this in actionscript3 private function uploadFile(event:MouseEvent):void { var uploader:URLRequest
I have a ComboBox with a label function like this one: private function fieldLabelFunction(item:Object):String
Say I have a class like this: class Person { private $value; public function
I'm trying to get json code from page.I use this private function __ajax_admin_search($username =
How would I convert the following to a VB.NET predicate using Array.Find? Private Function
Private Function GetWebDataGridOKButtonId() As String Dim ctls As ControlCollection = _ WebDataGrid1.Controls(0).Controls(0).Controls Dim btn
private function dataLevel():void { //Level 2 a1=new Array(b1,b2); a2=new Array(b3,b4); //Level 1 allA=new Array(a1,a2);
I have a label function like : private function formatDate (item:Object, column:DataGridColumn):String { var
If I have a method such as: private function testMethod(param:string):void { // Get the
I have the following serialization method: Private Function SerializeData(ByVal data As cData) As String

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.