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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:03:13+00:00 2026-05-14T20:03:13+00:00

So, I have a table like such: id|root|kw1|kw2|kw3|kw4|kw5|name 1| A| B| C| D| E|

  • 0

So, I have a table like such:

id|root|kw1|kw2|kw3|kw4|kw5|name
 1|   A|  B|  C|  D|  E|  F|fileA
 2|   A|  B|   |   |   |   |fileB
 3|   B|  C|  D|  E|   |   |fileC
 4|   A|  B|   |   |   |   |fileD

(several hundred rows…)

And I need to get it into a tree like the following:

*A
 *B
  -fileB
  -fileD
 *C
  *D
   *E
    *F
     -fileA
*B
 *C
  *D
   *E
    -fileC

I’m pretty sure the table is laid out poorly but it’s what I have to live with.

I’ve read a little about Adjacency List Model & Modified Preorder Tree Traversal but I don’t think my data is laid out correctly. I think this requires a recursive function, but I’m not at all sure how to go about that.

I’m open to any ideas of how to get this done even if it means extracting the data into a new table just to process this.

Are there any good options available to me or any good ways to do this? (Examples are a bonus of course)

  • 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-14T20:03:14+00:00Added an answer on May 14, 2026 at 8:03 pm

    Here’s the simplest working solution I could come up with.

    Assumptions:

    1. You have an array of arrays (your result set); it is named $rows.
    2. Empty column values in the result set you give above are equal to null.
    3. There are no branches in your tree whose names are string representations of integers.

    The code:

    $tree = array();
    
    foreach($rows as $row) {
        // Second parameter: array of 6 items as per your sample result set
        place_in_tree($tree, array($row['root'], ... $row['kw5']), $row['file']);
    }
    
    function place_in_tree(array $tree, array $path, $item) {
        // While there are more branches to be taken in $path
        while(($branch = array_shift($path)) !== null) {
            // Create the new branch if it doesn't exist
            if(!isset($tree[$branch])) {
                $tree[$branch] = array();
            }
    
            // Select the subtree in that branch for the next iteration
            $tree = $tree[$branch];
        }
    
        // Finally, add the item
        $tree[] = $item;
    }
    

    This creates an array with nested arrays. This array contains a number of items with string keys (these are “branches”, and are of type array) and a number of items with numeric keys (these are “files”, and are of type string). Child arrays are populated in the same way.

    If you require something more close to your business model than a big honking array, you can tweak the branch selection and item storage logic in function place_in_tree above.

    Also, if assumption #3 above does not hold in your case, you will need to get a little involved in the same manner, either choosing a non-ambiguous way to tell apart the branches and leaves of the tree, or choosing another structure to represent it.

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

Sidebar

Related Questions

I have table with 50 entries (users with such details like Name Surname Location
I have a table like this: name code group john 12 smith 15 how
i have a table like this one: -------------------------------- id | name -------------------------------- 1 |
I have two table like this table_CN (_id, name, phone, favorite, title) table_EN (_id,
Say that I have a table like that: name | age a | 1
I have a table like the following table: UserID Num1 Num2 Code Name Cat
I have a table like : trans is the name of the table for
i have a table with 3 columns id,name and parent_id representing categories. The root
I have a table I'm working with that is like such: <table width=100% border=0
If i have two tables like user table-u userid | name 1 | lenova

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.