Skip to content
Advertisement

How to create multidimensional array unlimited depth array with parent and child came from single table

Im trying to create multi-dimesional array with unlimited depth. I select the data from the database and check it if the field ‘isArray’ is true, meaning this column is a parent then I tried to make a loop to make it look for its child 'parent_id' => $row->id.

I’m expecting output like this.

JavaScript

And the data I get from the database is this enter image description here

I came up with this code:

JavaScript

But I ended up with this array:

JavaScript

Advertisement

Answer

Credits to Recursive function to generate multidimensional array from database result

I solved my problem with this code

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement