i have a flat array with values like:

array[0] = "/fol1_level1"
array[1] = "/fol1_level1/fol1_level2"
array[2] = "/fol1_level1/fol1_level2/fol1_level3"
array[3] = "/fol2_level1"
array[4] = "/fol2_level1/fol2_level2"

...
i am trying to convert this array in to a nested one by using some kind of recursive logic that nests all related values for example:

array[0][/fol1_level1]
array[0][/fol1_level1][/fol1_level2]
array[0][/fol1_level1][/fol1_level2][/fol1_level3]
array[1][/fol2_level1]
array[1][[/fol2_level1][fol2_level2]

...
its a directory tree only thing is all the directories are stored in a database.

    Well I was curious to see if I could do this, and the method that I came up with is... well, rather odd (I had to use eval() to start with). Here's the code:

    foreach($array as $key => $value) {
    	unset($array[$key]);
    	eval('$array['.$key.']' . makeArray($value) . ' = NULL;');
    }
    
    function makeArray($value) {
    	$arrays = array_filter(explode('/', $value));
    	for($i=reset($arrays), $tmp=''; $i !== FALSE; $i=next($arrays))
    		$tmp .= "['$i']";
    	return $tmp;
    }

    and with your example array, this is the output:

    Array
    (
        [0] => Array
            (
                [fol1_level1] => 
            )
        [1] => Array
            (
                [fol1_level1] => Array
                    (
                        [fol1_level2] => 
                    )
            )
        [2] => Array
            (
                [fol1_level1] => Array
                    (
                        [fol1_level2] => Array
                            (
                                [fol1_level3] => 
                            )
    
                )
    
        )
    [3] => Array
        (
            [fol2_level1] => 
        )
    [4] => Array
        (
            [fol2_level1] => Array
                (
                    [fol2_level2] => 
                )
        )
    )

      I wanna have a turn.

      Okay, this is the second attempt; the first was ghastly and convinced me there had to be a cleaner solution, probably involving recursion.

      function make_arrays($array)
      {
      	foreach($array as $k=>$path)
      	{
      		$array[$k] = array_filter(explode('/', $path));
      	}
      	return make_arrays_aux($array);
      }
      
      function make_arrays_aux($array)
      {
      	$new_array = array();
      	foreach(array_filter($array) as $path)
      	{
      		$dir = array_shift($path);
      		$new_array[$dir][]=$path;
      	}
      	if(!count($new_array)) return null;
      	return array_map('make_arrays_aux',$new_array);
      }
      

      The output of that is

      Array
      (
          [fol1_level1] => Array
              (
                  [fol1_level2] => Array
                      (
                          [fol1_level3] =>
                      )
      
          )
      
      [fol2_level1] => Array
          (
              [fol2_level2] =>
          )
      
      )

        Thanks ... ill try it out ....

          This should do it ...

          //create the nested array
          foreach ($all_physical_folders as $path) {
          	eval('$paths[\''.implode('\'][\'', array_filter(explode('/', $path))).'\'] = "";');
          }
          
            Write a Reply...