为什么每当我使用scandir()时我都会在数组的开头接收句点?
Array
(
[0] => .
[1] => ..
[2] => bar.php
[3] => foo.txt
[4] => somedir
)
Array
(
[0] => somedir
[1] => foo.txt
[2] => bar.php
[3] => ..
[4] => .
)
Run Code Online (Sandbox Code Playgroud)
phi*_*hag 97
每个目录列表中都有两个条目:
. 指的是当前目录.. 指父目录(或根目录,如果当前目录是根目录)您可以通过将它们从scandir的结果中过滤掉,从结果中删除它们:
$allFiles = scandir(__DIR__); // Or any other directory
$files = array_diff($allFiles, array('.', '..'));
Run Code Online (Sandbox Code Playgroud)
在一行代码中:
$files=array_slice(scandir('/path/to/directory/'), 2);
Run Code Online (Sandbox Code Playgroud)
要删除.,并..从scandir使用此功能:
function scandir1($dir)
{
return array_values(array_diff(scandir($dir), array('..', '.')));
}
Run Code Online (Sandbox Code Playgroud)
该array_values命令对数组重新索引,使其从0开始。如果您不需要对数组重新索引,则可接受的答案会很好地工作。只需:array_diff(scandir($dir), array('..', '.'))。
| 归档时间: |
|
| 查看次数: |
29968 次 |
| 最近记录: |