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

The Archive Base Latest Questions

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

In my heart, I feel that there must be a super simple recursive solution

  • 0

In my heart, I feel that there must be a super simple recursive solution to this, but I cannot immediately grok it.

I have a tree stored in SQL as a closure table. The tree looks like: (1 (2 (3), 4)), and the languages are MySQL’s SQL and PHP 5.3.

The closure table is thus:

+----------+------------+
| ancestor | descendant |
+----------+------------+
|        1 |          1 | 
|        2 |          2 | 
|        3 |          3 | 
|        4 |          4 | 
|        1 |          2 | 
|        1 |          3 | 
|        1 |          4 | 
|        2 |          3 | 
+----------+------------+

I can query the ancestors quite easily with:

 SELECT descendant AS id, GROUP_CONCAT(ancestor) as ancestors FROM
 closure GROUP BY (descendant);

 +----+-----------+
 | id | ancestors |
 +----+-----------+
 |  1 | 1         | 
 |  2 | 2,1       | 
 |  3 | 3,1,2     | 
 |  4 | 4,1       | 
 +----+-----------+

How can I easily build a tree in PHP with this data? Can I use a smarter query to pull more of the data from MySQL?

  • 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-11T23:27:56+00:00Added an answer on May 11, 2026 at 11:27 pm

    The first key is to sort the SQL results by the number of ancestors. I did this in PHP since I avoid the complexities of multi-digit numbers.

    This provides a list of nodes in an order in which they can be validly inserted.

    Array
    (
        [1] => Array
            (
                [0] => 1
            )
    
        [4] => Array
            (
                [0] => 4
                [1] => 1
            )
    
        [2] => Array
            (
                [0] => 2
                [1] => 1
            )
    
        [3] => Array
            (
                [0] => 3
                [1] => 1
                [2] => 2
            )
    
    )
    

    At this point, I don’t care about the keys, only the lists of ancestors. The path through the tree can be found between the intersection of available nodes and the remaining ancestors.

      function add_node($ancestors, &$tree) {
        if (count($ancestors) == 1) {
          $tree[array_pop($ancestors)] = array();
          return;
        }   
        $next_node = array_intersect($ancestors, array_keys($tree));
        $this->add_node(
            array_diff($ancestors, $next_node) , 
            $tree[array_pop($next_node)]
            );  
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I feel silly asking this question... Sometimes I have a domain that I want
If I have an object created as autoreleased, is there a way that I
I'm planning software that's an OLAP application at its heart (it helps analyse metering
I know this is probably a question that is asked a ton on here
If you moved to a new programming language, which libraries do you feel must
I remember reading in Douglas Crockford's Javascript the Good Parts book that there is
I know I have to consider about mail header injection , and are there
I feel really silly to not be able to find this answer anywhere. Could
So I have a MySQL query that is being created dynamically based on parameters
We have a small 3 developer team that is currently using Subversion for our

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.