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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:20:43+00:00 2026-05-26T23:20:43+00:00

Is there a way of storing and working (adding ,removing etc..) with tree data

  • 0

Is there a way of storing and working (adding ,removing etc..) with tree data structure in PHP and Mysql?

Can PHP’s RecursiveItrator Itrator be of any use here?

So basically I wanna have tree structures, to store some hierarchies like Categories of some products that can go on indefinitely, It would be very nice to be able to store everything in database, fetch them and do simple things like BFS and DSF traversals in them.

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

    A good way to get all the nodes for a given tree when you use Adjacency List is to add a column to every row called root_id or something, so every node knows not only its immediate parent, but also the top node in its tree.

    CREATE TABLE Comments (
      comment_id INT PRIMARY KEY,
      root_id INT,
      parent_id INT,
      FOREIGN KEY (root_id) REFERENCES Comments (comment_id),
      FOREIGN KEY (parent_id) REFERENCES Comments (comment_id)
    );
    

    So if you have a hierarchy of 10 -> 20 -> 30, you’d store the following:

    INSERT INTO Comments SET comment_id = 10, root_id = 10;
    INSERT INTO Comments SET comment_id = 20, root_id = 10, parent_id = 10; 
    INSERT INTO Comments SET comment_id = 30, root_id = 10, parent_id = 20; 
    

    This is similar to how Slashdot stores trees of comments for example.

    If you can write a query to fetch all the nodes of a given tree, and each node knows its immediate parent, in the style of the Adjacency List design of storing hierarchical data, you can convert the query result set into a multidimensional array or tree of objects as you fetch it.

    See my answer to Convert flat array to the multi-dimentional for code to do this in PHP.

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

Sidebar

Related Questions

Is there some way I can define String[int] to avoid using String.CharAt(int) ?
I'm working with push notifications on a device. Is there a way to set
When searching, is there a way to disable scoring for any query? The scenario
Is there a way to convert a HTML string into a Image .tiff file?
Is there any way to parse a string in the format HH:MM into a
Is there a way to run a regexp-string replace on the current line in
Is there any way to format a string by name rather than position in
Is there a way to take substrings of a string with .bat/.cmd files? For
Is there a way to get the raw SQL string executed when calling PDOStatement::execute()
Is there a way to change the connection string of a DataBase object in

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.