Struggling with this task and will explain below. I have some data that I fetch from MySQL database. I then prettify the array (remove keys with empty children) to look like so:
Array
(
[1] => Array
(
[title] => Categories
[id] => 2
[children] => Array
(
[0] => Lyrical
[1] => Jazz
[2] => Tap
)
)
[2] => Array
(
[title] => Group Divisions
[id] => 3
[children] => Array
(
[0] => Small Fry
[1] => Junior
[2] => Cutie
)
[parent] => 1
)
[3] => Array
(
[title] => Classifications
[id] => 1
[children] => Array
(
[0] => Professional/Teacher
[1] => Student Choreography
)
[parent] => 2
)
)
What I need is to convert it to nested array of the following structure:
Array
(
[0] => Array
(
[title] => Lyrical
[children] => Array
(
[0] => Array
(
[title] => Small Fry
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
[1] => Array
(
[title] => Junior
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
[2] => Array
(
[title] => Cutie
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
)
)
[1] => Array
(
[title] => Jazz
[children] => Array
(
[0] => Array
(
[title] => Small Fry
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
[1] => Array
(
[title] => Junior
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
[2] => Array
(
[title] => Cutie
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
)
)
[2] => Array
(
[title] => Tap
[children] => Array
(
[0] => Array
(
[title] => Small Fry
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
[1] => Array
(
[title] => Junior
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
[2] => Array
(
[title] => Cutie
[children] => Array
(
[0] => Array
(
[title] => Professional/Teacher
)
[1] => Array
(
[title] => Student Choreography
)
)
)
)
)
)
NOTE: we can potentially have an unlimited number of keys in the initial array, not necessarily 3. For example, if we have 5 keys instead of 3, the resulting nested array should have 5 "branches".
Any help is appreciated. Thanks in advance!