我试图使用“。”从可变字符串创建一个多维数组。作为分隔符,我试图使用“。”进行爆炸。但这并没有给出我正在寻找的结果
输入
file1
file1.file2
file1.file3
file1.file3.file4
file5
file5.file6
我在找
的结果array("file1" => array("0" => "file1", file1.file2 = array("0" => "File2"), array("file1.file3" => array("0" => "file3", "file1.file3.file4" => array("0" =>"file4")),"file5" => array("0"=>"file5", "file5.file6" => array("0" => "file6")));
如果有人能够帮助我会非常感激
干杯
Vip32
答案 0 :(得分:0)
function AddFolder(array & $array, $string)
{
$path = explode(".", $string);
$aux =& $array;
foreach($path as $key)
{
$patkey[] .= $key;
$pathkey = implode(".", $patkey);
if(isset($aux[$pathkey]))
{
$aux =& $aux[$pathkey];
}else{
$aux[$pathkey] = ["0" => $key];
$aux =& $aux[$pathkey];
}
}
}
$arraylist = array();
foreach($list as $line)
{
AddFolder($arraylist, $line);
}