I have an array that I populate with parents, children, children of children, children of children of children and so on and so forth. However I can't seem to figure out how to do that for as many levels as their are without having to write each level out.
First now I have
foreach($this->tree as $k=>$v) {
if($v['id'] == $i['pid']) {
// Add children
$this->tree[$k]['children'][] = array('name'=>$i['name'],'id'=>$i['id']);
break;
}else{
foreach($v['children'] as $kc=>$vc) {
$this->tree[$k]['children'][$kc]['children'][] = array('name'=>$i['name'],'id'=>$i['id']);
}
}
}
Which generates something that looks like
Array
(
[0] => Array
(
[name] => Test
[id] => 1
[children] => Array
(
[0] => Array
(
[name] => Test2
[id] => 2
[children] => Array
(
[0] => Array
(
[name] => Test 3
[id] => 3
)
)
)
)
)
);
But I can't seem to figure out how to do is without writing out a billion foreach loops.
$i is just four rows, id, pid (parent id), name, and weight.