[ create a new paste ] login | about

Link: http://codepad.org/KHI3Qrzi    [ raw code | output | fork ]

PHP, pasted on Sep 6:
<?php
    $files = array('dir1/dir2/dir3/file1', 
                'dir1/dir2/dir3/file2', 
                'dir1/dir2/dir3/file3',
                'dir1/dir2/dir3/file4'
    );
    		
    		
    $newFiles = array();
    foreach($files as $file){
    	$one = explode('/', $file);
    	$last = array_pop($one);
    	$rev = array_reverse($one);
    	$mixArray = array();
    	foreach($rev as $num => $dir){
    		if($num < count($rev)){
    			$mixArray[$dir] = $last;
    			$last = $mixArray;
    			if($num < count($rev)-1){
    				unset($mixArray);
    			}
    		}
    	}
    	$newFiles = array_merge_recursive($newFiles, $mixArray);
    }
    
    var_export($newFiles);


Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
array (
  'dir1' => 
  array (
    'dir2' => 
    array (
      'dir3' => 
      array (
        0 => 'file1',
        1 => 'file2',
        2 => 'file3',
        3 => 'file4',
      ),
    ),
  ),
)


Create a new paste based on this one


Comments: