小编nic*_*888的帖子

如何将文件列表作为数组,其中key与value相同?

我可以用一些帮助.我必须从一个目录获取文件列表,并将它们作为数组返回,但键需要与值相同,因此输出将如下所示:

array( 
    'file1.png' => 'file1.png', 
    'file2.png' => 'file2.png', 
    'file3.png' => 'file3.png' 
) 
Run Code Online (Sandbox Code Playgroud)

我找到了这段代码:

function images($directory) {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);

    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

        // if file isn't this directory or its parent, add it to the results
        if ($file != "." && $file != "..")
        {
            $results[] = $file;
        }

    }

    // tidy …
Run Code Online (Sandbox Code Playgroud)

php arrays file list

1
推荐指数
1
解决办法
164
查看次数

标签 统计

arrays ×1

file ×1

list ×1

php ×1