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 7751283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:30:28+00:00 2026-06-01T11:30:28+00:00

I have a situation where I have already obtained and manipulated SQL data into

  • 0

I have a situation where I have already obtained and manipulated SQL data into an array and into a tree. I am trying to avoid recursion at all costs because it has bitten me in the behind in the past.

I have an array of elements with Parent_IDs, and I’d like to be able to obtain a list of all of their children and subchildren. It shouldnt be nearly as complex as going the other way (from an array to a nested tree) which I got without issue using references, but for some reason I’m brain-frozen…

Any help appreciated.

I have the data in two possible formats because I have manipulated it already. Whichver is best for input can be used. this is a structure (print_r) of the two arrays I have:

Array
(
    [202735] => Array
        (
            [ID] => 202735
            [text] => aaafdf
            [Parent] => 
        )

    [202737] => Array
        (
            [ID] => 202737
            [text] => Filho 2
            [Parent] => 202735
        )

    [202733] => Array
        (
            [ID] => 202733
            [text] => Neto 1
            [Parent] => 202731
        )

    [202739] => Array
        (
            [ID] => 202739
            [text] => Neto 2
            [Parent] => 202737
        )

)

or

Array
(
    [0] => Array
        (
            [ID] => 202735
            [text] => aaafdf
            [Parent] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [ID] => 202737
                            [text] => Filho 2
                            [Parent] => 202735
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [ID] => 202739
                                            [text] => Neto 2
                                            [Parent] => 202737
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [ID] => 202733
            [text] => Neto 1
            [Parent] => 202731
        )

)

Desired output in the format:
(first level parent => all children and grandchildren)

array(202731=>array(202735));
array(202735=>array(202737,202739));

or similar… Ideally i’ll wrap this in a function like ListChildren($InitialParent), and return all children from such… invoking ListChildren(0) or (null) would list all elements and all subs…

(additional array elements can be ignored for the purpose of this exercise)

OBS: some data in the array is missing… namely the category above 202735, which would be 202731, but thats just because i limited the data I copied in… basically I can have either a flat array with parent ids, or a “tree” array with nested children as the source.

  • 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-01T11:30:29+00:00Added an answer on June 1, 2026 at 11:30 am

    I ended up with this. Thanks for the help, in the end I reverted to a recursive code. I’ll monitor it for performance although I suppose just within the PHP layer it wont be an issue. I had a bad experience when I went in for maintenance on a project that had DB calls in a recursive function… as the usage grew the recursvie function usage grew exponentially and the DB calls went to 100s of thousands…

    anyways:

        function __GetChildrenRec($Lista, $Categoria){
            // Return false if $initialParent doesn't exist
            if ($Categoria == 0) $Categoria = "";
            if (!isset($Lista[$Categoria])) return FALSE;
    
            // Loop data and assign children by reference
            foreach ($Lista as $CategAtual) {
                if ($CategAtual[Parent] == $Categoria) {
                    $Filhos[] = $CategAtual[ID];
                    $Filhos = array_merge((array)$Filhos,(array)self::__GetChildrenRec($Lista, $CategAtual[ID]));
                }
            }
    
            // Return the data
            return is_array($Filhos) ? $Filhos : array();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation similar to the following question: Insert Data Into SQL Table
I have a situation that requires redirecting users who are already logged in away
I have already posted a question about this, but the situation has changed sufficiently
I have situation where a user can manipulate a large set of data (presented
I have next situation: 1) N workers, all workers do same things 2) Each
Working with a relatively large data set I have a situation where I need
Here's the situation: subversion is already installed in the server and I have access
We have a situation in our product where for a long time some data
I have a situation where I have two arrays and I use one array(A)
I have a similar situation to this question , which was already posted. Koraktor

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.