I have array
$arr = ['one', 'two', 'free'];
$value = 10;
Need use recursion function.
So I wanna get array like this
$result = ['one' => ['two' => ['free' => 10]]];
I have array
$arr = ['one', 'two', 'free'];
$value = 10;
Need use recursion function.
So I wanna get array like this
$result = ['one' => ['two' => ['free' => 10]]];
You can loop the array in reverse and build the array from the furthest nested value and back.
$arr = ['one', 'two', 'free'];
$value = 10;
$arr = array_reverse($arr);
foreach($arr as $key => $val){
if($key==0){
$new[$val]= $value;
}else{
$new = [$val => $new]; //this overwrites the array with a new array with another layer
}
}
var_dump($new);