It's great, but hard to understand and seems like you don't care about performance. You might be looking for something like: http://www.browse-tutorials.net/tutorial/create-treeview-using-recursion-in-php