tags:

views:

66

answers:

3

I have this array:

[@attributes] => Array
                (
                    [url] => 
                    [navigation] => true
                    [template] => home
                )

            [@children] => Array
                (
                    [0] => Array
                        (
                            [name] => Home
                        )

                    [1] => Array
                        (
                            [desc] => Lorem ipsum dolor
                        )

                    [2] => Array
                        (
                            [@children] => Array......

I need it to instead be this:

[@attributes] => Array
                (
                    [url] => 
                    [navigation] => true
                    [template] => home
                )

            [@children] => Array
                (
                      [name] => Home
                      [desc] => Lorem ipsum dolor
                      [@children] => Array......

I can't change the way this array is actually being built unfortunately so I need to basically get rid of each item being in their own array, they should all be nested under children.

Thanks in advance

+2  A: 

First loop through the children, storing each value in a new array, then overwrite it:

$newChildren = array();
foreach ($arr['@attributes']['@children'] as $property) {
  foreach ($property as $key => $val) {
    $newChildren[$key] = $val;
  }
}
$arr['@attributes']['@children'] = $newChildren;

If these values are nested deeper than one level (I see another @children key/array), you can put this in a function and call it recursively.

Peter Kruithof
+2  A: 

You can do this using call_user_func_array and array_merge:

$arr['@children'] = call_user_func_array('array_merge', $arr['@children']);

This will call array_merge with the arrays of $arr['@children'] as parameters.

Gumbo
A: 

Try something along the lines of ...

foreach (@attributes[@children] as $arrayVal)
{
   foreach($arrayVal as $key=>$val)
        @mySimpleArray[] = $val

}
Jas