I have an array, the array key contains the file name and array value will be another array which contain sfile size,date etc.
In these array list, I want to store only xls and xlsx files alone. I need to remove files of all other extensions.
Is there any predefined function in PHP?
Note: i have constant variable this varaible contain the like 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
)
in this example i want remove only sample.txt array keys and its corresponding values.
$filtered = array_intersect_key(
$array,
array_filter(array_keys($array), function ($name) { return preg_match('/\.xlsx?$/', $name); })
);
$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;
}
}
Then you use $new_array.