One dimensional array to array of arrays

If I have a function like this:

function convertArray($input)
{

}

How would I code it so it would produce results like the following?


$result = convertArray( array('a', 'b', 'c', 'd') );
// $result should be array( 'a' => array( 'b' => array( 'c' => array( 'd' => array() ) ) ) )

$result = convertArray( array('x', 'y', 'z') );
// $result should be array( 'x' => array( 'y' => array( 'z' => array() ) ) )

Is this possible?

Reverse the array, then loop through it and create your new array.

or recurse it to do the same.

Thanks!

“Reverse the array” gave me my eureka moment.

Here is what I have come up. It seems to work.

function convertArray(array $input = array())  
{ 
    $result = array();
    while(count($input)){
        $newArray = array();
        $newArray[array_pop($input)] = $result;
        $result = $newArray;
    }
    return $result;
}