Jump to content

Multi-Dimensional Table Sort


benwilhelm

Recommended Posts

Hello All,

 

I'm new to the forum and have a sticky problem that I've been working on for a couple days now.  I have a database table with a self-referencing foreign key, creating a hierarchy of items, like so:

 

iditem_namebelongs_to

=============================

1Category_Foonull

2Category_Barnull

3Foo_11

4Foo_21

5Bar_12

6Bar_22

5Foo_1_Sub3

6Bar_1_Sub5

 

I'm trying to write a function that takes this table (currently as a one-dimensional array) and creates a multi-dimensional array out of it.  Like so:

 

[item_1] => array(
                    [id] => 1
                    [name] => Category_Foo
                    [children][0] => array(
                                         [id] => 3
                                         [name] => Foo_1
                                         [belongs_to] => 1
                                         [children][0] => array(
                                                              [id] => 5
                                                              [name] => Foo_1_Sub
                                                              [belongs_to] => 3
                                                       )
                                  )
                    [children][1] => array(
                                         [id] => 3
                                         [name] => Foo_2
                                         [belongs_to] => 1
             )
[item_2] => // ...and so on

 

I feel like there is probably a very elegant solution that I'm missing.  Thanks in advance for any help.

 

Ben

Link to comment
https://forums.phpfreaks.com/topic/184926-multi-dimensional-table-sort/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.