用于 wathcfolder 的 php 格式数组


Php format array for wathcfolder

如何格式化一个数组,该数组在基本的html表中保存文件夹(监视文件夹)的文件夹。

这是基本功能

function dirToArray($dir) {
    $result = array();
    $cdir = scandir($dir);
    foreach ($cdir as $key => $value)
    {
        if (!in_array($value,array(".","..")))
        {
            if (is_dir($dir . DIRECTORY_SEPARATOR . $value))
            {
                $result[$value] = $this->dirToArray($dir . DIRECTORY_SEPARATOR . $value);
            }
            else
            {
                $result[] = $value;
            }
        }
    }
    return $result;
}

我这样称呼它;

$watchFolderPath = 'C:''xampp''htdocs''mop''Rp''';
$watchFolder     = $this->dirToArray($watchFolderPath);

如果我print_r这个,它会给我正确的值,在这种情况下是带有子文件夹的文件夹。看起来像这样:

array:6 [▼
  345 => array:2 [▶]
  789 => array:2 [▶]
  "folder1" => array:2 [▶]
  "folder2" => array:2 [▶]
  "folder3" => array:2 [▶]
  "folder4" => []
]

我不知道如何正确格式化它。

您可以简单地echo与数组结果混合的HTML标记:

echo "<table>";

for ($row = 0; $row < count($watchFolder); $row++)
    echo "<tr>";
    echo "<td>" . $watchFolder[$row] . "</td>";
    for ($col = 0; $col < 2; $col++) {
        echo "<td>" . $watchFolder[$row][$col] . "</td>";
    }
    echo "</tr>";
}
echo "</table>";

按照您认为合适的方式排列表格 - <tr>标签是行,<td>标签是数据项。

编辑:

要处理没有子文件夹的文件夹,也许可以在循环中尝试类似的东西。

    if is_array($watchFolder[$row]) {
        for ($col = 0; $col < 2; $col++) {
            echo "<td>" . $watchFolder[$row][$col] . "</td>";
        }
    }