tags:

views:

22

answers:

2

How could I move all nodes of the 'fields' array into its parent array '113', whilst unsetting 'fields' ?

    [a] => Array
    (
        [113] => Array
            (
                [title] => asdfasdfas
                [alias] => asdfasdfas
                [fields] => Array
                    (
                        [jr_streetaddress] => Array
                            (
                                [type] => text
                                [label] => Street Address
                                [data] => asdfasdffsd
                            )

                        [jr_towncity] => Array
                            (
                                [type] => text
                                [label] => Town / City
                                [data] => Nottingham
                            )
                    )
            )
     )
A: 

If you can make this array like this:

[a] => Array
(
    [113] => Array
        (
            [title] => asdfasdfas
            [alias] => asdfasdfas
            [jr_streetaddress] => Array
                        (
                            [type] => text
                            [label] => Street Address
                            [data] => asdfasdffsd
                        )

            [jr_towncity] => Array
                        (
                            [type] => text
                            [label] => Town / City
                            [data] => Nottingham
                        )
        )
 )

try to use this code:

$array['a'][113]['jr_streetaddress'] = $array['a'][113]['fields']['jr_streetaddress'];
$array['a'][113]['jr_towncity'] = $array['a'][113]['fields']['jr_towncity'];
unset($array['a'][113]['fields']);
Alexander.Plutov
+2  A: 

Assuming your top level array ($something['a']) is the variable $a:

foreach($a as $key => $values){
  if(isset($values['fields']))
    {
       $a[$key] = array_merge($a[$key], (array) $values['fields']);
       unset($a[$key]['fields']);
    }
}

Alternatively, if you dont want to hit every array element in $a you can just remove the loop and substitute $values with $a[113] and $key with 113.

Also note the casting for the fields element to an array, jsut in case it isnt one with (array) $values['fields']

prodigitalson