0

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.

6
  • How do you store your file names? Commented May 15, 2013 at 7:19
  • yes, only after these validation Commented May 15, 2013 at 7:23
  • I mean are there stored in some array with keys having file names eg. array('sample.xls'=>array(...), 'sample.csv'=>array(...)? Commented May 15, 2013 at 7:24
  • 1
    Instead of removing unwanted entries - why are you adding them? Always try to fix the reason, not hide the symptoms. (if you have control about the building of the array.) Commented May 15, 2013 at 7:26
  • 1
    Basically it is not possible without iterating over the array, which you say you do not want to do. Commented May 15, 2013 at 7:38

2 Answers 2

1
$filtered = array_intersect_key(
    $array,
    array_filter(array_keys($array), function ($name) { return preg_match('/\.xlsx?$/', $name); })
);
Sign up to request clarification or add additional context in comments.

Comments

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;
    }
}

Then you use $new_array.

2 Comments

so if i have 1000 files in array, then loop will round for 1000 times. as well i dont want pregmatch here, withjust path to i can get extension .... but i dont want to round 1000 time, i want to use some array function with ext as argument, that should take of filteration
You have to loop. Even if you use a function that PHP provides, it will loop over the array. What you can do is, if you only need the array once, is to do your process inside the "foreach" of the code I provided. Then you won't need to loop twice (one to filter, another to process the array).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.