lostnucleus Posted November 13, 2010 Share Posted November 13, 2010 Hi consider I have following type of directory structure inside /root folder1 -fileName1.1 -folder1.2 --fileName1.2.1 $dirItr = new RecursiveDirectoryIterator('/root'); $itr = new RecursiveIteratorIterator($dirItr); $structure ; // the type of structure i want to build store inside it foreach($itr as $object) { //magic happens here } //in the end I want folowing type of structure $structure = array('label'=>'folder1','children'=>array(array('label'=>'fileName1.1'),array('label'=>'folder1.2','children'=>array(array('label'=>'fileName1.2.1'))))); Importan observations; a)children key is an array which contains arrays of other folder or filename; b) filename does not have children attribute because they are leafs of tree c) a folder without any files will be represented as array('label'=>'emptyFolderName',children=>array()) ; //children attribute with empty array. I wd be really thankfull to the user whoever solved this problem , wasted a month already on it !! thanks in Advance. Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/ Share on other sites More sharing options...
kenrbnsn Posted November 13, 2010 Share Posted November 13, 2010 Please post the code you're having problems with. We don't write code for you, but we will help you solve your coding/logic problems. Ken Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1133785 Share on other sites More sharing options...
lostnucleus Posted November 13, 2010 Author Share Posted November 13, 2010 @kenrbnsn its an logical problem , i want to convert directory tree into specific array format , which I have showed you with example , code will be written only if there is a solution for this logical problem !! Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1133836 Share on other sites More sharing options...
lostnucleus Posted November 13, 2010 Author Share Posted November 13, 2010 bump !!! Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1133845 Share on other sites More sharing options...
salathe Posted November 13, 2010 Share Posted November 13, 2010 First, please don't bump! Your thread is not more important than any other in this forum. Now, on to being helpful. I think you're approaching things from the wrong direction. The easiest way to get a nested array structure out of a recursive iterator like that is to build it in pieces with a simple recursive function (see below). The RecursiveIteratorIterator is designed to make it easy to move over a recursive structure, in a "flat" way, which is entirely not what you want here. Here's a recursive formatting function which takes in a RecursiveDirectoryIterator and spits out the nested array that you would like. The idea is very simple. function array_formatter(RecursiveDirectoryIterator $iterator) { $array = array(); foreach ($iterator as $fileinfo) { // Directories and files have labels $current = array( 'label' => $fileinfo->getFilename() ); // Only directories have children if ($fileinfo->isDir()) { $current['children'] = array_formatter($iterator->getChildren()); } // Append the current item to this level $array[] = $current; } return $array; } P.S. If you have any questions, thoughts, opinions on using the recursive iterators please do feel free to start other threads (and maybe ping me about them). I'm generally more than happy to go on and on about them—anything to get more folks aware of, and using, them—mostly as penance for not finishing the documentation in the PHP manual yet. Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1133856 Share on other sites More sharing options...
lostnucleus Posted November 13, 2010 Author Share Posted November 13, 2010 First, please don't bump! Your thread is not more important than any other in this forum. Now, on to being helpful. I think you're approaching things from the wrong direction. The easiest way to get a nested array structure out of a recursive iterator like that is to build it in pieces with a simple recursive function (see below). The RecursiveIteratorIterator is designed to make it easy to move over a recursive structure, in a "flat" way, which is entirely not what you want here. Here's a recursive formatting function which takes in a RecursiveDirectoryIterator and spits out the nested array that you would like. The idea is very simple. function array_formatter(RecursiveDirectoryIterator $iterator) { $array = array(); foreach ($iterator as $fileinfo) { // Directories and files have labels $current = array( 'label' => $fileinfo->getFilename() ); // Only directories have children if ($fileinfo->isDir()) { $current['children'] = array_formatter($iterator->getChildren()); } // Append the current item to this level $array[] = $current; } return $array; } P.S. If you have any questions, thoughts, opinions on using the recursive iterators please do feel free to start other threads (and maybe ping me about them). I'm generally more than happy to go on and on about them—anything to get more folks aware of, and using, them—mostly as penance for not finishing the documentation in the PHP manual yet. You have identified my problem well , but just missing one major part of it I want returning array to follow same hierarchy structure as that of direcorty iterator !! children key of array acts as a subfolder !! Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1133859 Share on other sites More sharing options...
lostnucleus Posted November 13, 2010 Author Share Posted November 13, 2010 any php genious who can solve my problem ?? Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1133868 Share on other sites More sharing options...
salathe Posted November 13, 2010 Share Posted November 13, 2010 You have identified my problem well , but just missing one major part of it I want returning array to follow same hierarchy structure as that of direcorty iterator !! children key of array acts as a subfolder !! The "missing" part to get the exact same array as in your first post is a super-simple change to the example that I gave to get the ball rolling. That is, if I'm understanding your exclamations correctly: it is very difficult to decipher what you want as you're really not being clear (whether you know that or not). The function that I gave follows the structure (except for the root item, which doesn't make a whole lot of sense to include that special case) as you described it and I'm not at all sure what you mean by it needing "to follow same hierarchy structure as that of direcorty [sic] iterator !! children key of array acts as a subfolder !!" any php genious who can solve my problem ?? Your impatience is tiresome. Wait for replies or you certainly will not be getting any more. Anyway, it doesn't take a genius to help you; please set the bar a little lower. Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1133871 Share on other sites More sharing options...
lostnucleus Posted November 14, 2010 Author Share Posted November 14, 2010 see you have analyzed my problem well , but I dont want flatten array , the whole purpose is to build multi dimensional arrays to mock the directory structure .Arrays inside other just as folder is inside other folder in directory structure. Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1134013 Share on other sites More sharing options...
salathe Posted November 14, 2010 Share Posted November 14, 2010 I give up. If you're not willing to read what people are saying when they're helping you, then there is really no point in trying. Quote Link to comment https://forums.phpfreaks.com/topic/218574-recursive-iterator-iterator-tree-problem/#findComment-1134022 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.