I want to convert a xml script into a associative array in PHP. The XML script is :

```
<ages>
    <Peter>
        <Peterchild>4</Peterchild>
        <Peterchild>6</Peterchild>
    </Peter>
    <Quagmire>30</Quagmire>
    <Joe>34</Joe>
</ages>

```

and the code i wrote for converting it to an array is

```
${$xml->getName()} = array();
foreach($xml->children() as $child){
    $ages[$child->getName()] = (string)$child;
}

```

which gives the output as

```
Array
(
    [Peter] => 

    [Quagmire] => 30
    [Joe] => 34
)

```

The problem is that I am unable to figure out a condition to recursively traverse through the children of children (children of Peter in this example). How to I change this code to consider those nodes as well?