Wrapping certain elements in an array with an array with PHP
You can try something like this, note that it is untested:

&lt;?php
function createControls($parentView) {
    $newArr = $parentView;
    foreach ($parentView as $k =&gt; $v) {
        $newArr[$k] = ['type' =&gt; 'control'];
        if (isset($v['childViews'])) {
            $v = createControls($v['childViews']);
        }
        $newArr[$k]['childViews'] = $v;
    }
    return $newArr;
}
?&gt;


This function uses reccursion to do the same as your code above. It takes one array of Key Value pairs and reassigns the values to wrap the old values into an array with the following format:

array ( 'type' =&gt; 'control', 'childViews' =&gt; $oldValue );


Basically, instead of nesting a new loop each time the depth of your input array changes like in your function above this function will just pass the old childValues back into itself and assign the result as the new childValues array.