我使用此代码在HTML表中打印子文件夹的名称和大小,我要做的就是将此表另存为2d数组,以便可以按文件夹大小进行排序(因为现在按字母名称顺序进行排序)
<?php
$directory = "F:/directory";
echo "<table>";
$depth = 0;
$count = 0;
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach ( $ritit as $splFileInfo ) {
$count +=1;
if ($ritit->getDepth() === $depth && $splFileInfo->isDir()) {
echo "<tr><td>".stripslashes($splFileInfo)."</td>";
echo "<td>".getSize($splFileInfo)."</td></tr>";
}
}
echo "</table>";
function getSize($dir, $precision = 2) {
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
$bytes = 0;
foreach ( $ritit as $v ) {
$bytes += $v->getSize();
}
$bytes = max($bytes, 0);
return round($bytes, $precision) . ' ';
}
?>
这是HTML,左侧为文件夹名称,右侧为文件夹大小 GraphQLError: Syntax Error: Expected :, found {
答案 0 :(得分:0)
将文件名存储为键,将文件大小存储为$ r 2D数组中的值。使用sort函数对使用值对数组进行排序。
以下是对数组进行排序的链接:https://www.w3schools.com/php/php_arrays_sort.asp
<?php
$directory = "F:/directory";
echo "<table>";
$depth = 0;
$count = 0;
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS), RecursiveIteratorIterator::CHILD_FIRST);
$r = array();
foreach ( $ritit as $splFileInfo ) {
$count +=1;
if ($ritit->getDepth() === $depth && $splFileInfo->isDir()) {
// echo "<tr><td>".stripslashes($splFileInfo)."</td>";
// echo "<td>".getSize($splFileInfo)."</td></tr>";
$r[stripslashes($splFileInfo)] = getSize($splFileInfo);
}
}
// sort the associative array using the value.
asort($r);
foreach($r as $key => $value) {
echo "<tr><td>".$key."</td>";
echo "<td>".$value."</td></tr>";
}
echo "</table>";
function getSize($dir, $precision = 2) {
$ritit = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS));
$bytes = 0;
foreach ( $ritit as $v ) {
$bytes += $v->getSize();
}
$bytes = max($bytes, 0);
return round($bytes, $precision) . ' ';
}
?>