function createTree($tree,$branch,$leaf){
foreach($tree as $key=>$row){
if($row[$leaf])
$children[$tree[$key][$leaf]][] = $row;
else
$parent[] = $row;
}
$tree = $parent;
unset($parent);
$children = array_values($children);
foreach($children as $childGroup){
$childGroup = array_reverse($childGroup);
foreach($childGroup as $child){
foreach($tree as $key=>$row){
if($row[$branch] == $child[$leaf]){
array_splice($tree,$key+1,0,array($child));
}
}
}
}
return $tree;
}
My testing led me to splitting up the different rows into two separate arrays (first foreach).
The parent array is a single level array that holds all categories that have no parent, sort of the "base" array for the end.
The children array is split into a two dimensional array, where we just stack each row from the tree that has a parent category. I did this by just referring to the category_parent as the key.
(eg).
If two categories both have a category_parent of 14 then we add both rows to $children[14][->row information here<-]
if a category only has category_parent of 1 then we add that row to $children[1][->row information here<-].
By doing this it separates the categories into subcategories itself, using the multi-demen array.
Next: use array_values to re-index the children array (this way we don't have $children[1], $children[14], etc...)(They re-index to [0],[1],etc...). This way we don't run into any breaks in the array.
set $tree to $parent and unset $parent.
Now the tricky part. A triple foreach 🙁
First, iterate through $children array, but since it is a multidimensional array we have to do again.
I used array_reverse, immediately after first foreach. This is because on my sqli statement I order by category_name. If i reverse the order (Zed to Alpha), then when I splice out the array, I can just continue to push the subcategory down (assuming that there multiple sub-categories) and everything comes out Alpha to Zed when the array is dumped.
Second, iterate through the second level of children array that already has the subcategories grouped for me by array.
Third, iterate through the parent(which is now tree) array and splice that bad boy open and insert my child row at an index of $key+1 of the parent array.
Return $tree.
**IMPORTANT::: THE PROBLEM WITH THIS is that if a subcategory OF A subcategory gets served up before the first sub-category gets put into the "main" array, then it either gets lost or shows up last in the array and doesn't get sorted properly.
** SOLUTION::: The category id of a sub->sub->category MUST be higher than that of the category id of a sub->category. This will usually be the case anyways because logically, not always the case, you will enter a sub->category in before you enter a sub->sub->category. Otherwise its like saying your going to have grandchildren before you have children.
Applogies, kind of lengthy, but I didn't want to restrict my db table to have to use the nested model as described on dev.mysql and have to worry about multiple top level categories and sub-categories being out of bounds, and lft and rght 's, so I think this is a feasible solution, and works in my instance. Hopes this finds its way into someone else's hands and they might find a possible solution to the problem as stated above.
Cheers,
Samuel