Suppose we have an array:

$b = ['a', 'b', 'c',];

It is clear how to make a maximum of a two-dimensional array of it, pass through a cycle

 for($x = 0; $x < 5; $x++) { $q[$x] = ['$b[$x]' => ' ']; } 

Is it possible to make a multi-dimensional array of it, arrays nested into each other:

 $a = ['a' => [ 'd' => [ 'c' => '']]]; 
  • there were such questions already that I can’t find (( - - teran

3 answers 3

 $array = ['a', 'b', 'c']; $array = array_reverse($array); $result = []; foreach ($array as $value) { $result = [ $value => $result ]; } print_r($result); 

    Of course

     $b = ['a', 'b', 'c',]; $a = []; $tmp = &$a; foreach($b as $v) { $tmp[$v] = []; $tmp = &$tmp[$v]; } var_dump($a); 

      Maybe you wanted to?

       $arr = []; $b = ['a', 'b', 'c']; $link = &$arr; for($i=0, $len=sizeof($b); $i<$len; $i++){ $link = [$b[$i]=> []]; $link = &$link[$b[$i]]; } var_dump($arr); ?>