Recursive Hierarchy Tree

Hi All,

I am looking for some advice on a recursive method I am trying to add into a resource controlled to return flat SQL data in a tree like format for the backend.

I am aware this has been posted before as I have used one post but it does not seem to work for me as follows is my data and desired output :-

id parent_id text

1 0 Parent
2 1 Child
3 2 Grandchild
4 0 Parent 2
5 4 Child 2
5 1 Child

Tree Required :-

– Child
— Grandchild
– Child
Parent 2
– Child 2

I did try the following post and suggestion from [@bobbybouwmann] :-

function buildTree(array $elements, $parentId = 0) {
    $branch = array();  
foreach ($elements as $element) {
   if ($element['parent_id'] == $parentId) {
        $children = buildTree($elements, $element['id']);
        if ($children) {
            $element['children'] = $children;
        $branch[] = $element;

return $branch;


$tree = buildTree($rows);

My data array is passing to the method fine and I can dd the elements right up until $children when I try to re use the method. The result of $branch is an empty array.

I understand how this is meant to work but am a little puzzled as to why I get an empty data set. I am happy if it is an array of data or collection preferably a collection.

As I do not have a limit or cannot foresee how deep the tree may go I wanted to make a dynamic loop of the table so I could output this in order in nested ‘ul’ containers so it would work regardless of the depth.

Pretty sure someone will have done something similar to the above just wondering anyone else’s thoughts on this and if they have experienced similar issues.

Thanks in advance Jamie

The code works as expected, there must be something wrong with your input array.

Hi Chorn,

Thanks for the information my current array is an array converted from a collection. So I pass a variable to the function with all the data from the table :-

    ['id' => 1, 'parent_id' => 0, 'text' => 'Parent'],
    ['id' => 2, 'parent_id' => 1, 'text' => 'Child'],
    ['id' => 3, 'parent_id' => 2, 'text' => 'Grandchild'],

Is this how you tested or is your Array slightly different? No problem if not just be good to see the working array to see what I am doing wrong.


here’s what i’ve tested

Hi Chorn,

How strange, I will double check my data if this is correct, if it is maybe it is because I am using OOP.

So buildTree() is a method and I am calling $this->buildTree($elements, $element[‘id’]) inside the method but would not have thought this would matter or effect the data.

Thanks for your input mate.


You can just copy and paste to your code the example i linked and rewrite it to OOP code step by step until you encounter any error. Then you can use var_dump to check variables and conditions. Or you can just var_export your input data and use this as testdata for copy and pasting it in

1 Like

Hi Chorn,

You were correct it all worked fine I was trying to output/view the tree within the function itself outside of the loop. However as the function is run in the loop this is what was causing the issue!

So silly mistake from me, again thanks for your help mate really appreciated certainly put me on the right tracks.


Sponsor our Newsletter | Privacy Policy | Terms of Service