1

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?

2
  • You call $this->createArray($child, $group['children']); - but you're not actually doing anything with the data returned from this function? Commented May 4, 2017 at 11:51
  • It will be used to return the big array in the AJAX function. Commented May 4, 2017 at 11:53

2 Answers 2

3

Your current structure seems unneccessary complicated. Why pass the children as reference to your function? You just have to return all elements where the id is your parent_id and append.

function createArray($parent_id) {
    $t = [];
    foreach ($this->getNavigationItems($parent_id) as $group) {
        // do wathever you want with group...
        // now call this method recursive and store the result in children
        $group['children'] = createArray($group['id']);
        $t[] = $group;
    }
    return $t;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Where '$t' is my big array? As far as I know, this works for 2 levels: the parent and the children. But what if the children have children too?
Because of that, createArray calls itself recursive - the $t in your example is called $array
Oops, sorry, my head wasn't really working. Thanks for this answer, it works really well. Cheers!
0

Figured it out already:

public function createArray($parent_id = null, &$array = array())
{
    foreach ($this->getNavigationItems($parent_id) as $group)
    {
        $child = $group['child'];
        $children = array();
        $group['children'] = &$children;
        $array[] = $group;

        if ($child)
        {
            $this->createArray($child, $children);
        }
    }

    return $array;
}

I had to make the array parameter a reference. Also I had to make a separate variable for the children. The reference of that variable will be used as $group['children']. $children will be used as new parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.