I've been trying to unflatten an array with a recursive lambda function but I can't seem to get my head around it.
$test = function( $a, $b ) use ( &$test ) {
if ( ! count($a) ) return $b;
$b[array_shift($a)] = []; // Missing logic here.
return $test( $a, $b );
};
$newArr = $test( [0, 1, 2], [] );
echo "<pre>";
print_r($newArr);
echo "</pre>";
That's the code but I have no idea what to do in the "// Missing logic here." part.
I'd like this recursive lambda function to convert
[0, 1, 2]
Into:
Array
(
[0] => Array
(
[1] => Array
(
[2] => test
)
)
)
[0, 1, 2]to[0 => [1 => [2 => 'test']]], when "test" is not specified anywhere.