我有一个像这样的数组目录树。
键是完整文件路径,值是文件大小。
Array
(
[directory1\file1.jpg] => 197602
[directory1\file2.jpg] => 274893
[directory2\file1.txt] => 41
[directory2\file2.txt] => 41
[directory2\sub-directory1\file1.txt] => 47
[directory2\sub-directory1\file2.txt] => 41
[directory2\sub-directory1\file3.txt] => 47
[file1.txt] => 41
[file2.txt] => 47
)
我试图像这样使用ul li创建目录树。
<ul class="filetree">
<li class="closed"><span class="folder">directory1</span>
<ul>
<li class="hvr"><span class="file">file1.jpg</span><span></span><span class="right">197602</span></li>
<li class="hvr"><span class="file">file2.jpg</span><span></span><span class="right">274893</span></li>
</ul>
</li>
<li class="closed"><span class="folder">directory2</span>
<ul>
<li class="closed"><span class="folder">sub-directory1</span>
<ul>
<li class="hvr"><span class="file">file1.txt</span><span></span><span class="right">47</span></li>
<li class="hvr"><span class="file">file2.txt</span><span></span><span class="right">41</span></li>
<li class="hvr"><span class="file">file3.txt</span><span></span><span class="right">47</span></li>
</ul>
</li>
<li class="hvr"><span class="file">file1.txt</span><span></span><span class="right">41</span></li>
<li class="hvr"><span class="file">file2.txt</span><span></span><span class="right">41</span></li>
</ul>
</li>
<li class="hvr"><span class="file">file1.txt</span><span></span><span class="right">41</span></li>
<li class="hvr"><span class="file">file2.txt</span><span></span><span class="right">47</span></li>
</ul>
以上,每个目录和文件都使用<li>
但只是一个不同的类,但其他目录中的文件/目录使用<ul> <li>
使用foreach循环。
<?php
$array = Array
(
'directory1\file1.jpg' => 197602,
'directory1\file2.jpg' => 274893,
'directory2\file1.txt' => 41,
'directory2\file2.txt' => 41,
'directory2\sub-directory1\file1.txt' => 47,
'directory2\sub-directory1\file2.txt' => 41,
'directory2\sub-directory1\file3.txt' => 47,
'file1.txt' => 41,
'file2.txt' => 47
);
print_r($array);
foreach($array as $path => $size){
if(strpos($path,'\\') !== false) {
//file is under a directory tree/structure
$directory_tree = explode('\\',$path);
//create directories using li class
//last element in array is filename.
print_r($directory_tree );
}else{
//its just file, no directories to create
echo '<li class="hvr"><span class="file">'.$path.'</span><span></span><span class="right">'.$size.'</span></li>';
}
}
如何跟踪已创建的目录,或者我必须规范化上述数组,
喜欢
Array
(
[directory1] Array(
[file1.jpg] => 197602
[file2.jpg] => 274893
)
[directory2]Array(
[file1.txt] => 41
[file2.txt] => 41
[sub-directory1]Array(
[file1.txt] => 41
[file2.txt] => 44
[file3.txt] => 54
)
)
[file1.txt] => 41
[file2.txt] => 47
)
因此根据数组中的元素创建html会更容易。
答案 0 :(得分:2)
尝试这样的事情:
$array = array(
'directory1\file1.jpg' => 197602,
'directory1\file2.jpg' => 274893,
'directory2\file1.txt' => 41,
'directory2\file2.txt' => 41,
'directory2\sub-directory1\file1.txt' => 47,
'directory2\sub-directory1\file2.txt' => 41,
'directory2\sub-directory1\file3.txt' => 47,
'file1.txt' => 41,
'file2.txt' => 47
);
$result=array();
foreach ($array as $key => $value) {
if (strpos($key,'\\') !== false) {
$key = explode("\\", $key);
if(isset($key[1]) && !isset($key[2])){
$result[$key[0]][$key[1]] = $value;
}
if(isset($key[2])){
$result[$key[0]][$key[1]][$key[2]] = $value;
}
}else{
$result[$key] = $value;
}
}
print_r($result);
<强>输出强>
Array
(
[directory1] => Array
(
[file1.jpg] => 197602
[file2.jpg] => 274893
)
[directory2] => Array
(
[file1.txt] => 41
[file2.txt] => 41
[sub-directory1] => Array
(
[file1.txt] => 47
[file2.txt] => 41
[file3.txt] => 47
)
)
[file1.txt] => 41
[file2.txt] => 47
)