0

我有一个数组,数组键包含文件名,数组值将是另一个包含 sfile 大小、日期等的数组。

在这些数组列表中,我只想单独存储xlsxlsx文件。我需要删除所有其他扩展名的文件。

PHP中有任何预定义的函数吗?

注意:我有常量变量,这个变量包含类似 VAR_FILE_EXT= "xls, xlsx, csv"

[sample.xls] => Array (
            [size] => 3353
            [uid] => 1012
            [gid] => 1013
            [permissions] => 33188
            [atime] => 1338497357
            [mtime] => 1338497357
            [type] => 1
        )

[sample.csv] => Array (
            [size] => 3353
            [uid] => 1012
            [gid] => 1013
            [permissions] => 33188
            [atime] => 1338497357
            [mtime] => 1338497357
            [type] => 1
        )

[sample.txt] => Array (
            [size] => 3353
            [uid] => 1012
            [gid] => 1013
            [permissions] => 33188
            [atime] => 1338497357
            [mtime] => 1338497357
            [type] => 1
        )

在此示例中,我只想删除 sample.txt 数组键及其相应的值。

4

2 回答 2

1
$filtered = array_intersect_key(
    $array,
    array_filter(array_keys($array), function ($name) { return preg_match('/\.xlsx?$/', $name); })
);
于 2013-05-15T07:19:33.567 回答
1
$your_array = array(
    'sample.xls' => ...
    'sample.csv' => ...
    'sample.txt' => ...
);

$extensions = '/('.str_replace(', ', '|', $VAR_FILE_EXT).')$/i';
$new_array = array();

foreach ($your_array as $file => $stuff) {
    if (preg_match($extensions, $file)) {
        $new_array[$file] => $stuff;
    }
}

然后你使用 $new_array。

于 2013-05-15T07:23:44.923 回答