N-Bomb(Nerd) Posted January 16, 2012 Share Posted January 16, 2012 Hello, I have a string that contains a directory path much like: "/images/icons" and I want to take that string through a function and explode it into an array of directories that with add my directory structure array: imagesicons Then if I were to send the string "/data/documents" to the function it would append to the existing array: imagesicons [*]data documents Here's the code that I've got thus far: <?php $global_structure = array(); generate_structure("./images/icons"); generate_structure("./data/documents"); function generate_structure($directory) { if(!is_dir($directory)) return false; $directory_structure = explode("/", $directory); // do something here to iterate over $global_structure and add new directories to equal to current directory path making the value an empty array. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/255177-split-string-to-array/ Share on other sites More sharing options...
N-Bomb(Nerd) Posted January 16, 2012 Author Share Posted January 16, 2012 I forgot to mention that I want the generate_structure() function to be recursive so I can pass long directory paths and have it append to the array regardless of the depth. That way I'm able to pass in a directory path like "/images/icons/big/massive/giant/small/" and it would work. Quote Link to comment https://forums.phpfreaks.com/topic/255177-split-string-to-array/#findComment-1308366 Share on other sites More sharing options...
Morpheous Posted January 16, 2012 Share Posted January 16, 2012 So you are looking for nested arrays, in the following manner: $global_structure[0] = array(array("images",array("icons")), "data",array("documents")) If I have understood correctly, I think you may find this approach has a lot of overhead. Have you thought of creating each folder as an Object, where each Object has a parent? Quote Link to comment https://forums.phpfreaks.com/topic/255177-split-string-to-array/#findComment-1308367 Share on other sites More sharing options...
teynon Posted January 16, 2012 Share Posted January 16, 2012 Is it possible for there to be a file on the end? If so, is this directory something on your server? IE can you run stuff like is_file on it? Either way, this has no need to be recursive. Quote Link to comment https://forums.phpfreaks.com/topic/255177-split-string-to-array/#findComment-1308369 Share on other sites More sharing options...
teynon Posted January 16, 2012 Share Posted January 16, 2012 This essentially does what you ask, but I think you need to think about whatever it is you are trying to do. <?php $GLOBALS['STRUCT'] = array(); function structure($array) { $folders = explode("/", $array); $current = &$GLOBALS['STRUCT']; foreach ($folders as $folder) { if (!empty($folder) && $folder != "." && $folder != "..") { $current[$folder] = array(); $current = &$current[$folder]; } } } $dir = "/poop/in/a/basket/"; structure($dir); print_r($GLOBALS['STRUCT']); ?> EDIT: Furthermore, you shouldn't be using globals here at all. Sorry I even posted it. <?php $structure = array(); function structure($array, $structure) { $folders = explode("/", $array); $current = &$structure; foreach ($folders as $folder) { if (!empty($folder) && $folder != "." && $folder != "..") { $current[$folder] = array(); $current = &$current[$folder]; } } return $structure; } $dir = "/poop/in/a/basket/"; $structure = structure($dir, $structure); print_r($structure); ?> Quote Link to comment https://forums.phpfreaks.com/topic/255177-split-string-to-array/#findComment-1308371 Share on other sites More sharing options...
Andy-H Posted January 16, 2012 Share Posted January 16, 2012 function generate_structure($path) { static $structure = array(); static $index = 0; $last = substr($path, strrpos($path, '/') + 1); $next = substr($path, 0, strrpos($path, '/')); if ( empty($last) ) generate_structure($next); if ( !isset($structure[$index]) ) $structure[$index] = array(); $structure[$index][] = $last; if ( empty($next) ) { $structure[$index] = array_reverse($structure[$index]); $index++; return $structure; } generate_structure($next); return $structure; } generate_structure('/path/to/myfile/file.php'); generate_structure('/path/to/myfile/file.php'); generate_structure('/path/to/myfile/file.php'); $f = generate_structure('/path/to/myfile/file.php'); var_dump($f); Output: array 0 => array 0 => string 'path' (length=4) 1 => string 'to' (length=2) 2 => string 'myfile' (length=6) 3 => string 'file.php' (length= 1 => array 0 => string 'path' (length=4) 1 => string 'to' (length=2) 2 => string 'myfile' (length=6) 3 => string 'file.php' (length= 2 => array 0 => string 'path' (length=4) 1 => string 'to' (length=2) 2 => string 'myfile' (length=6) 3 => string 'file.php' (length= 3 => array 0 => string 'path' (length=4) 1 => string 'to' (length=2) 2 => string 'myfile' (length=6) 3 => string 'file.php' (length= Quote Link to comment https://forums.phpfreaks.com/topic/255177-split-string-to-array/#findComment-1308390 Share on other sites More sharing options...
teynon Posted January 17, 2012 Share Posted January 17, 2012 I take back what I said about rethinking this. This function is actually somewhat nice: <?php $structure = array(); function structure($dir, &$structure) { $folders = explode("/", $dir); $current = &$structure; foreach ($folders as $folder) { if (!empty($folder) && $folder != "." && $folder != "..") { if (!isset($current[$folder])) { $current[$folder] = array(); } $current = &$current[$folder]; } } } $dir = array(); $dir[] = "/poop/in/a/basket/"; $dir[] = "/poop/in/a/toilet/"; $dir[] = "/poop/bowl/"; $dir[] = "/random/directory"; $dir[] = "C:/Program Files"; $dir[] = "C:/Program Files/Windows Media Player"; $dir[] = "C:/Program Files/Microsoft Games"; $dir[] = "C:/Program Files/Microsoft Games/Chess"; $dir[] = "C:/Program Files/Microsoft Games/Chess/en-US"; $dir[] = "C:/Program Files/Microsoft Games/FreeCell"; $dir[] = "C:/Program Files/Microsoft Games/FreeCell/en-US"; $dir[] = "C:/Program Files/Microsoft Games/Hearts"; $dir[] = "C:/Program Files/Microsoft Games/Hearts/en-US"; foreach ($dir as $d) { structure($d, $structure); } echo "<pre>"; print_r($structure); echo "</pre>"; ?> Output: Array ( [poop] => Array ( [in] => Array ( [a] => Array ( [basket] => Array ( ) [toilet] => Array ( ) ) ) [bowl] => Array ( ) ) [random] => Array ( [directory] => Array ( ) ) [C:] => Array ( [Program Files] => Array ( [Windows Media Player] => Array ( ) [Microsoft Games] => Array ( [Chess] => Array ( [en-US] => Array ( ) ) [FreeCell] => Array ( [en-US] => Array ( ) ) [Hearts] => Array ( [en-US] => Array ( ) ) ) ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/255177-split-string-to-array/#findComment-1308436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.