coverting "." serparated string to an array ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

coverting "." serparated string to an array ??

Post by PHPycho »

Hello forums!!
Suppose we had list of strings in an array in a hierarchical form(separated by ".") as follows:

Code: Select all

FOLDER1
FOLDER1.subfolder1
FOLDER1.subfolder2
FOLDER1.subfolder2.sub_subfolder1
FOLDER1.subfolder2.sub_subfolder12
.. can go upto n levels
FOLDER2
FOLDER2.subfolder1
I want to convert such string in an array , For example

Code: Select all

array("FOLDER1" => array("subfolder1", "subfolder2" => array("sub_subfolder1","sub_subfolder12"), ...), "FOLDER2" => array("subfolder1"), ...etc)
How to accomplish this as it seems to be recursive.
Thanks in advance for the valueable tips and suggestions.
User avatar
Sekka
Forum Commoner
Posts: 91
Joined: Mon Feb 18, 2008 10:25 am
Location: Huddersfield, West Yorkshire, UK

Re: coverting "." serparated string to an array ??

Post by Sekka »

Firstly, how are you wanting this finished array to be structured? Because the example you posted is chaotic at best.

For example, this is the structure of your example finished array,

Code: Select all

Array
(
    [FOLDER1] => Array
        (
            [0] => subfolder1
            [subfolder2] => Array
                (
                    [0] => sub_subfolder1
                    [1] => sub_subfolder12
                )
 
        )
 
    [FOLDER2] => Array
        (
            [0] => subfolder1
        )
 
)
As you can see the key structure keeps changing and where you store the 'folder names' is changing too. May I suggest the following format instead?

Code: Select all

Array
(
    [FOLDER1] => Array
        (
            [subfolder1] => false
            [subfolder2] => Array
                (
                    [sub_subfolder1] => false
                    [sub_subfolder12] => false
                )
 
        )
 
    [FOLDER2] => Array
        (
            [subfolder1] => false
        )
        
    [FOLDER3] => false
 
)
As you can see, in this one, the folders are the keys for the array, and if they contain sub folders, that is then the value for that key.

So, to answer your original question, to parse your array of strings into this format, you can use the following function,

Code: Select all

function createStructure ($strings) {
    
    if (!is_array ($strings)) { return false; }
    
    $finalarray = array ();
    
    foreach ($strings as $string) {
        
        $folders = split ("\.", $string);
        $currentfolder = &$finalarray;
        
        foreach ($folders as $folder) {
            
            if (!is_array ($currentfolder)) {
                $currentfolder = array ();
            }
            
            if (!isset ($currentfolder[$folder])) {
                $currentfolder[$folder] = false;
            }
            
            $currentfolder = &$currentfolder[$folder];
            
        }
        
    }
    
    return $finalarray;
    
}
FYI, if the strings are in a file and not an array, load the text file then do a split("\n",$file); to get the strings into an array.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Re: coverting "." serparated string to an array ??

Post by PHPycho »

Thanks Sekka....
That code rocked..
Hats off..
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: coverting "." serparated string to an array ??

Post by pickle »

explode() might be a better choice than split(), as explode() doesn't use regular expressions - which you don't need for this application.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply