Jump to content

Recursive Iterator Iterator Tree problem ??


lostnucleus

Recommended Posts

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.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 !!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.