Click to See Complete Forum and Search --> : coverting "." serparated string to an array ??


PHPycho
03-11-2008, 04:23 AM
Hello forums!!
Suppose we had list of strings in an array in a hierarchical form(separated by ".") as follows:
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
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.

wellsideas
03-11-2008, 10:21 AM
foreach ($lines as $line){
$line = split ('.',$array_line); // this will split the line into an array
$key = $line[0]; // store the first value as the key
$array[$key] = array_shift($line); take all of the array except the first element
}


Recursively call this function for each line... This should put you in the right direction, let me know