I've got a multidimensional array containing some id's, stored in keys called 'name'. Each entry can have other sub-arrays, containing other id's. The array is dynamic; the depth and entries are unknown. Here is an example:

```
Array
(
    [0] => Array
        (
            [name] => test1
            [subs] => Array
                (
                    [0] => Array
                        (
                            [name] => test2
                        )

                    [1] => Array
                        (
                            [name] => test3
                            [subs] => Array
                                   (
                                       [name] => test4
                                   )
                        )

                )

        )

    [1] => Array
        (
            [name] => test5
        )
)

```

Now I want to convert this multidimensional array to a 'flat' array, while keeping hold of the depth. The scope of the new array is some kind of table of contents, where the key represents a chapter and the value an id. For example, 'test4' should be chapter 1.2.1, 'test2' should be 1.1 and 'test5' should be chapter 2. Each level deeper means the entry is a child of the parent level. Therefore I have to store every previous depth-'level' while looping the array. So far I haven't found a way to do this.

**QUESTION UPDATE:**

I've got the first part working. Now I want to add new chapters to the array, and have the chapter numbers of the existing entries update themselves. The array now looks like this:

```
Array
(
    [1] => test1
    [1.1] => test2
    [1.2] => test3
    [1.2.1] => test4
    [2] => test5
)

```

So now I would like to add chapter 'test6' as first-child of 1.2, which means the current 1.2.1 would become 1.2.2 and the new child will be 1.2.1 instead.