Answer the question
In order to leave comments, you need to log in
How to save a tree from mysql to an array in php?
The database has a table of categories with parents and children. How to display it in php as an array?
Answer the question
In order to leave comments, you need to log in
For example like this sandbox.onlinephpfunctions.com/code/a3ee3d0e462cda...
If there is not a lot of data, you can do this:
1. Unload into a regular array of hashes with a regular select.
2. Set up a recursive function that receives the id of the tree node, and returns a hash whose keys are the id of the elements of the original table containing this node in the parent field, and the values are links to the result of applying the same function to the key.
For the first pass, I would calculate all the relationships for each element, the entire nesting depth.
By the second pass, I generated the tree itself, decomposing the elements into it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question