Jump to content

Help: Howto reorganize a multi-dimensional array?


AuntieSocial

Recommended Posts

Been cracking my head on this for a bit. help sorely needed! (I'm a php novice,please excuse :) )

 

I have a multi-dimensional representing a file in a particular directory and sub directories.

ex: I have

dir1/dir2/dir3/3.jpg

dir1/dir2/dir3/dir4/4.jpg

 

The array is set up as follows:

Array
(
     [0] => Array
     (
         [0] => dir1
         [1] => dir2
         [2] => dir3
         [3] => 3.jpg
     )

     [1] => Array
     (
         [0] => dir1
         [1] => dir2
         [2] => dir3
         [3] => dir4
         [4] => 4.jpg
     )     
)

 

what I'm looking to do is to create an array from the above array to represent the directory structure, something like this: (or  perhaps using only integer as keys?)

 

Array
(
    [dir1] => Array
      (
         [dir2] => Array
           (
                 [dir3] => Array
                   (
                         [0] => 3.jpg
                         [dir4] => Array
                           (
                              [0] => 4.jpg
                           )
                   )
           )
      )

)

 

how can this be achieved?

 

Would greatly appreciate help!

 

clueless

It can be done, I am quite interested why also like AV pointed out. This is how I would organize the array

 

$dirArray = array("dir1" => array(), "dir2" => array(), "dir3" => array("3.jpg"), "dir4" => array("4.jpg"));

 

Simple and easy.

 

If you MUST have it the way you have shown above look into recursion

 

http://en.wikipedia.org/wiki/Recursion

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.