开发者

php move nodes to parent array

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
                            )
                    )
            )
     )


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜