0

Possible Duplicate:
Recursive function to generate multidimensional array from database result

Consider the following array.

    Array
(
    [0] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 0
            [0] => string:val

        )

    [1] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 1
            [0] => string:val

        )

    [2] => Array
        (
            [Parent_ID] => 1
            [Child_ID] => 2
            [0] => string:val

        )

    [3] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 3
            [0] => string:val

        )

I need to get this into something like the following.

 Array
(
    [0] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 0
            [0] => string:val

        )

    [1] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 1
            [0] => string:val


                [2] => Array
                   (
                    [Parent_ID] => 1
                    [Child_ID] => 2
                    [0] => string:val

                    ) 
                   )

    [3] => Array
        (
            [Parent_ID] => 0
            [Child_ID] => 3
            [0] => string:val

        )

Further to that, Children can also have unlimited children. And not every index will have the same amount of keys and values. But all will have a parent and child key/value.

Iv'e tried numerous solutions from here and elsewhere and just cant seem to get it right.

2
  • 3
    Whats the logic on witch key to convert to sub array? Commented Nov 15, 2012 at 13:55
  • if Parent_ID matches an array, Place the matched array in its parent. Commented Nov 15, 2012 at 14:01

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.