Build a multidimensional array from an array in PHP

I would like to build a multidimensional array from an array. For example, I would like

$test = array ( 0 => 'Tree', 1 => 'Trunk', 2 => 'Branch', 3 => 'Limb', 4 => 'Apple', 5 => 'Seed' ); 

to become

 $test = array ( 'Tree' => array ( 'Trunk' => array ( 'Branch' => array ( 'Limb' => array ( 'Apple' => array ( 'Seed' => array () ) ) ) ) ) ); 

or more simply

 $result[Tree][Trunk][Branch][Limb][Apple][Seed] = null; 

I am trying to do this with a recursive function, but I push the memory limit, so I am obviously doing it wrong.

 <?php $test = array ( 0 => 'Tree', 1 => 'Trunk', 2 => 'Branch', 3 => 'Limb', 4 => 'Apple', 5 => 'Seed' ); print_r($test); print "results of function"; print_r(buildArray($test)); function buildArray (&$array, &$build = null) { if (count($array) > 0) { //create an array, pass the array to itself removing the first value $temp = array_values($array); unset ($temp[0]); $build[$array[0]] = $temp; buildArray($build,$temp); return $build; } return $build; } 
+6
source share
4 answers

This function works recursively and performs the trick:

 function buildArray($from, $to = []) { if (empty($from)) { return null; } $to[array_shift($from)] = buildArray($from, $to); return $to; } 

In your code, I expect you to see an error. You talk to $build in your first iteration, as if where the array was, while you defaulted to null .

+2
source

Here's an approach with foreach and no recursion that works:

 function buildArray($array) { $new = array(); $current = &$new; foreach($array as $key => $value) { $current[$value] = array(); $current = &$current[$value]; } return $new; } 

[ Demo ]

Now your function ... first, using $build[$array[0]] , without defining it as an array, first creates E_NOTICE . Secondly, your function goes into infinite recursion because you are not actually changing $array ( $temp not the same), so count($array) > 0 will be true for all eternity.
And even if you modified $array , you could no longer use $array[0] , because you canceled this, and the indexes will not just move up. For this you need array_shift .
After that, you pass $build and $temp to your function, which leads to the future, because now you assign $build to $temp , so you create another loop in an already endlessly repeating loop.

I tried to fix all of the above in your code, but in the end I realized that my code was now exactly the same as Pevara's answer , but with different variable names, so ... what is it.

+8
source

Seems easy

 $res = array(); $i = count($test); while ($i) $res = array($test[--$i] => $res); var_export($res); 

return

 array ( 'Tree' => array ( 'Trunk' => array ( 'Branch' => array ( 'Limb' => array ( 'Apple' => array ( 'Seed' => array ( ), ), ), ), ), ), ) 
+2
source

Using the pointer, continue re-pointing it deeper. Your two output examples gave array() and null for the deepest value; this gives array() , but if you want null , replace $p[$value] = array(); on $p[$value] = $test ? array() : null; $p[$value] = $test ? array() : null;

 $test = array( 'Tree', 'Trunk', 'Branch', 'Limb', 'Apple', 'Seed' ); $output = array(); $p = &$output; while ($test) { $value = array_shift($test); $p[$value] = array(); $p = &$p[$value]; } print_r($output); 
0
source

Source: https://habr.com/ru/post/987295/


All Articles