wathcfolder的Php格式数组

wathcfolder的Php格式数组,php,arrays,directory,watch,Php,Arrays,Directory,Watch,如何格式化在基本html表中保存文件夹(Watchfolder)文件夹的数组 这是基本功能 function dirToArray($dir) { $result = array(); $cdir = scandir($dir); foreach ($cdir as $key => $value) { if (!in_array($value,array(".",".."))) { if (is_

如何格式化在基本html表中保存文件夹(Watchfolder)文件夹的数组

这是基本功能

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);
如果我打印它,它会给我正确的值,在本例中是文件夹及其子文件夹。 看起来像这样:

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>";
echo”“;
对于($row=0;$row
根据需要排列表格-
标记是行,
标记是数据项

编辑:

要处理没有子文件夹的文件夹,可以在循环中尝试类似的操作

    if is_array($watchFolder[$row]) {
        for ($col = 0; $col < 2; $col++) {
            echo "<td>" . $watchFolder[$row][$col] . "</td>";
        }
    }
如果是数组($watchFolder[$row]){
对于($col=0;$col<2;$col++){
回显“$watchFolder[$row][$col]”;
}
}

我在第二秒得到一个未定义的偏移量:0,这可能是因为数组中的最后一个元素没有子数组。可以尝试添加检查,请参阅编辑原始注释。