note 91075 modified in ref.filesystem by danbrown

From: Date: Mon, 25 May 2009 22:15:56 +0000
Subject: note 91075 modified in ref.filesystem by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155994@lists.php.net to get a copy of this message
You have an array of directories (straightforward list of directories): <?php $array = array( '/home/drapeko/var', '/home/drapeko/var/y', '/home/drapeko', '/home', '/var/libexec' ); ); ?> And you would like to transform this array to hierarchy of directories: <?php $array = array ( 'home' => array ( 'drapeko' => array ( 'var' => array ( 'y' => array() ) ) ), 'var' => array( 'libexec' => array() ) ); ?> How can you do it? First of all the below function will help us. <?php /** * This function converts real filesystem path to the string array representation. * * for example, * '/home/drapeko/var/y will be converted to $result_array['home']['drapeko']['var']['y'] * '/home/drapeko/var/y/file.txt will be converted to $result_array['home']['drapeko']['var']['y'] * * @param $path realpath of the directory * @return string string array representation of the path */ function pathToArrayStr($path) { // TODO constants/configs? $res_path = str_replace(array(':/', ':\\', '/', '\\', DIRECTORY_SEPARATOR), '/', $path); // if the first or last symbol is '/' delete it (e.g. for linux) $res_path = preg_replace(array("/^\//", "/\/$/"), '', $res_path); // create string $res_path = '[\''.str_replace('/', '\'][\'', $res_path).'\']'; return $res_path; } ?> It simply converts the real path of the file to array string representation. How can you use this function? I know it looks like a little confusing. But it's quite simple. Consider the example below: <?php $result = array(); $check = array(); foreach($array as $val) { $str = pathToArrayStr($val, 'result'); foreach($check as $ck) { if (strpos($ck, $str) !== false) { continue 2; } } $check[] = $str; eval('$result'.$str.' = array();'); } print_r($result); ?> Heh, how do you find it? This approach has helped me very much. I hope you will find it useful. :) --was-- You have an array of directories (straightforward list of directories): $array = array( '/home/drapeko/var', '/home/drapeko/var/y', '/home/drapeko', '/home', '/var/libexec' ); ); And you would like to transform this array to hierarchy of directories: $array = array ( 'home' => array ( 'drapeko' => array ( 'var' => array ( 'y' => array() ) ) ), 'var' => array( 'libexec' => array() ) ); How can you do it? First of all the below function will help us. /** * This function converts real filesystem path to the string array representation. * * for example, * '/home/drapeko/var/y will be converted to $result_array['home']['drapeko']['var']['y'] * '/home/drapeko/var/y/file.txt will be converted to $result_array['home']['drapeko']['var']['y'] * * @param $path realpath of the directory * @return string string array representation of the path */ function pathToArrayStr($path) { // TODO constants/configs? $res_path = str_replace(array(':/', ':\\', '/', '\\', DIRECTORY_SEPARATOR), '/', $path); // if the first or last symbol is '/' delete it (e.g. for linux) $res_path = preg_replace(array("/^\//", "/\/$/"), '', $res_path); // create string $res_path = '[\''.str_replace('/', '\'][\'', $res_path).'\']'; return $res_path; } It simply converts the real path of the file to array string representation. How can you use this function? I know it looks like a little confusing. But it's quite simple. Consider the example below: $result = array(); $check = array(); foreach($array as $val) { $str = pathToArrayStr($val, 'result'); foreach($check as $ck) { if (strpos($ck, $str) !== false) { continue 2; } } $check[] = $str; eval('$result'.$str.' = array();'); } print_r($result); Heh, how do you find it? This approach has helped me very much. I hope you will find it useful. :) http://php.net/manual/en/ref.filesystem.php

« previous php.notes (#155994) next »