Q:

Build multidimensional array from keys

I can't seem to figure this out and I'm hoping someone has a magical recursive solution to this.  I have a list of keys and basically I want to transform it into a nested array. 
array('level1', 'level2', 'level3');

To
array(
    'level1' => array(
        'level2' => array(
             'level3' // last key in array should be just a value
        )
    )
)

Much appreciation to anyone who can help! 

A:

Something like this should do the job:
function buildMultiDimensional(Array $arr)
{
    // the first value will become a new key
    $newKey = array_shift($arr);

    if (empty($arr)) {
        // this is where the recursion stops
        return $newKey;
    }

    // and the recursion !!! 
    return array($newKey => buildMultiDimensional($arr));
}

$arr = array('level1', 'level2', 'level3', 'level4', 'level5');
var_dump(buildMultiDimensional($arr));

The result is what's expected:
array(1) {
    ["level1"]=>
  array(1) {
        ["level2"]=>
    array(1) {
            ["level3"]=>
      array(1) {
                ["level4"]=>
        string(6) "level5"
      }
    }
  }
}

