I have a database table with a parent/child system. It needs to be able to have unlimited levels (even though it might not be used, it needs to have it).
So I have a recursive function to create one big array. The array should look like:
array(
0 => array(
'id' => 1,
'name' => 'test1',
'children' => array(
0 => array(
'id' => 2,
'name' => 'test2',
'children' => array();
)
)
)
)
I currently have this function:
public function createArray($parent_id = null, $array = array())
{
foreach ($this->getNavigationItems($parent_id) as $group)
{
$child = $group['child'];
$group['children'] = array();
$array[] = $group;
if ($child)
{
$this->createArray($child, $group['children']);
}
}
return $array;
}
The table has a child and parent column. The child is used for parents, and the children will have the value of the child column of their parent as parent column value.
However, in my case the children array will be empty. So if I have 2 items, id 1 which has parent_id NULL and 2 which has parent_id 1, I will only get ID 1 with an empty children array, where it has to be an array containing ID 2.
What am I doing wrong?
$this->createArray($child, $group['children']);- but you're not actually doing anything with the data returned from this function?