benwilhelm Posted December 12, 2009 Share Posted December 12, 2009 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 More sharing options...
benwilhelm Posted December 12, 2009 Author Share Posted December 12, 2009 Oops. The table id's should read sequentially from 1-8. I accidentally repeated 5 and 6. But the problem remains. Any thoughts? Link to comment https://forums.phpfreaks.com/topic/184926-multi-dimensional-table-sort/#findComment-976239 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.