I am trying to make an associative array from a zip file, where the folders are the keys and the files are the innermost values.
Currently when I explode on /
on each entry in the zip file I get something like this:
Array
(
[0] => Folder1
[1] => Folder2
[2] => File1.txt
)
Array
(
[0] => Folder1
[1] => Folder2
[2] => File2.txt
)
Array
(
[0] => Folder1
[1] => Folder3
[2] => File3.txt
)
To achieve that I am doing this (doing a print_r
on $path
):
if(in_array($content["ext"], array("zip"))){
$zip = zip_open($file);
while($zip_entry = zip_read($zip)){
$zip_path = zip_entry_name($zip_entry);
$path = explode("/", $zip_path);
$path = array_filter($path);
$lastElement = end($path);
foreach($path as $key => $item){
if($item != $lastElement){
}
}
}
}
print_r($files);
What I would like is for $files
to contain a multidimensional array for a result that looks like this:
Array
(
[Folder1] => Array
(
[Folder2] => Array
(
[0] => File1.txt
[1] => File2.txt
)
[Folder3] => Array
(
[0] => File3.txt
)
)
)
What is the best way to achieve this without a depth?