You could take code snippets from this class , in particular a method _processContentEntry.
Here is a modified version of the method that performs the task:
function stringToArray($path)
{
$separator = '/';
$pos = strpos($path, $separator);
if ($pos === false) {
return array($path);
}
$key = substr($path, 0, $pos);
$path = substr($path, $pos + 1);
$result = array(
$key => stringToArray($path),
);
return $result;
}
Conclusion
var_dump(stringToArray('a/b/c/d'));
Will be
array(1) {
["a"]=>
array(1) {
["b"]=>
array(1) {
["c"]=>
array(1) {
[0]=>
string(1) "d"
}
}
}
}
I suppose you need :)
UPDATE
, , :
$string = "Folder1/Folder2/Folder3/filename1\n";
$string .= " Folder1/Folder2/Folder3/filename2\n";
$string .= " Folder4/Folder2/Folder3/filename3\n";
$lines = explode(PHP_EOL, $string);
$lines = array_map('trim', $lines);
$lines = array_filter($lines);
$output = array();
foreach ($lines as $line) {
$struct = stringToArray($line);
$output = array_merge_recursive($output, $struct);
}
print_r($output);
P.S.
, json_encode, , , .