Multidimensional array from string

前端 未结 3 674
故里飘歌
故里飘歌 2020-12-11 21:52

Let\'s say I have this string which I want to put in a multidimensional array.

Edit : The number of subfolders in the string are dynamic .. from zero sub folders to

3条回答
  •  囚心锁ツ
    2020-12-11 22:23

    I think this what you want,

    $string ="Folder1/Folder2/Folder3/filename1\n";
    $string .="Folder1/Folder2/Folder3/filename2\n";
    $string .="Folder4/Folder2/Folder3/filename3\n";
    
    
    $string_array_1 = explode("\n", $string);
    
    $array_need = array();
    
    foreach($string_array_1 as $array_values)
    {
            if($array_values)
            {
                $folders =  explode("/", $array_values);
                $array_need[$folders[0]][$folders[1]][$folders[2]][] = $folders[3];
            }
        }
    
    print_r($array_need);
    

提交回复
热议问题