Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
    
What have you tried so far and what error you are getting? –  bhargavg Sep 3 '14 at 4:19
    
I have no idea where to start. If there was a set number of keys (for example three, like in the example above), then this becomes very easy to achieve. With unlimited number of keys, I assume there should be some sort of "while" loop with original array modification (i.e. unset the key that was already used up fully) –  Stan Lee Sep 3 '14 at 4:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.