Hey everyone,
I'm a long time lurker, first time poster 🙂 I'm having a little trouble writing an algorithm to create a hierarchal structure from a database which looks like this:
parent_id | child_id |
----------------+-------------+
1 | 2 |
2 | 3 |
3 | 4 |
3 | 5 |
5 | 6 |
5 | 7 |
So coming out of the database, I get an array which looks like this:
Array
(
[0] => Array
(
[parent_id] => 1
[child_id] => 2
)
[1] => Array
(
[parent_id] => 2
[child_id] => 3
)
[2] => Array
(
[parent_id] => 3
[child_id] => 4
)
...
I would like to put this into an array which represents the following structure:
1
\
2
\
3
/ \
4 5
/ \
6 7
Can anyone point me in the right direction?
Thanks!
Ben