1

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
                            )
                    )
            )
     )
1
  • What is the "root" of this array? Do you have several of this subarrays or do you just want to do this for this particular array? Commented Sep 12, 2010 at 8:02

2 Answers 2

3

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']

Sign up to request clarification or add additional context in comments.

Comments

0

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']);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.