Answer the question
In order to leave comments, you need to log in
Nesting array elements?
There is a one-dimensional array of the form
[0] => dir2
[1] => dir2_1
[2] => dir2_1_1
...
array(
"dir2"=>array(
"dir2_1"=>array(
"dir2_1_1"=>array(...)
)
)
)
Answer the question
In order to leave comments, you need to log in
Can be recursed: https://3v4l.org/m5Cup
<?php
$input = ['dir2', 'dir2_1', 'dir2_1_1'];
function convert_array(array $input, array $output = []) {
if (empty($input)) {
return $output;
}
$value = array_pop($input);
return convert_array($input, [$value => $output]);
}
var_dump(convert_array($input));
<?php
$input = ['dir2', 'dir2_1', 'dir2_1_1'];
$output = [];
foreach (array_reverse($input) as $value) {
$output = [$value => $output];
}
var_dump($output);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question