15

I have a dynamically generated array of filenames, let's say it looks something like this:

$files = array("a-file","b-file","meta-file-1", "meta-file-2", "z-file");

I have a couple of specific filenames that I want discarded from the array:

$exclude_file_1 = "meta-file-1";
$exclude_file_2 = "meta-file-2";

So, I'll always know the values of the elements I want discarded, but not the keys.

Currently I'm looking at a couple of ways to do this. One way, using array_filter and a custom function:

function excludefiles($v)
        {
        if ($v === $GLOBALS['exclude_file_1'] || $v === $GLOBALS['exclude_file_2'])
          {
          return false;
          }
        return true;
        }

$files = array_values(array_filter($files,"excludefiles"));

Another way, using array_keys and unset:

$exclude_files_keys = array(array_search($exclude_file_1,$files),array_search($exclude_file_2,$files));
foreach ($exclude_files_keys as $exclude_files_key)
    {    
    unset($files[$exclude_files_key]);
    }
$files = array_values($page_file_paths);

Both ways produce the desired result.

I'm just wondering which one would be more efficient (and why)?

Or perhaps there is another, more efficient way to do this?

Like maybe there's a way to have multiple search values in the array_search function?

1
  • Hi Feanne - no need to change the title once you've found an acceptable answer. StackOverflow takes care of all that once you hit the green check-mark :)
    – Mike B
    Commented Apr 10, 2012 at 14:22

3 Answers 3

34

You should simply use array_diff:

$files = array("a-file","b-file","meta-file-1", "meta-file-2", "z-file");
$exclude_file_1 = "meta-file-1";
$exclude_file_2 = "meta-file-2";

$exclude = array($exclude_file_1, $exclude_file_2);
$filtered = array_diff($files, $exclude);

One of the bad things about PHP is that it has zillions of functions to do specific little things, but that can also turn out to be convenient sometimes.

When you come across a situation like this (you have found a solution after locating a relevant function, but you are unsure if there is something better), it's a good idea to browse the function list sidebar on php.net at leisure. Just reading the function names can pay huge dividends.

4
  • Thanks so much @Jon - I'll remember your advice. I do read the the function list at leisure, but never paid much attention to the "diff" functions because I couldn't imagine how I'd use them... now I know better! :)
    – Feanne
    Commented Apr 10, 2012 at 14:18
  • what if those elements are not strings but object or arrays? and I want to remove those elements which has some property eqlual to 'a' and 'b' for example?
    – Herokiller
    Commented Nov 2, 2015 at 10:34
  • @Herokiller in that case you need to go with array_filter.
    – Jon
    Commented Nov 2, 2015 at 11:26
  • 2
    Along similar lines, if you need to filter out specific keys from an associative array: $filtered = array_diff_key($arrayToFilter, array_flip(['remove_key_1', 'remove_key_2']));
    – John Rix
    Commented Jun 14, 2018 at 12:37
5

Use array_diff()

$files = array("a-file","b-file","meta-file-1", "meta-file-2", "z-file");
$exclude_file_array = array("meta-file-1", "meta-file-2");

will return an array with all the elements from $exclude_file_array that are not in $files.

$new_array = array_diff($files, $exclude_file_array);

Its better than your own function and loops.

2
  • Thank you, this has been pointed out and I have implemented it in my code.
    – Feanne
    Commented Apr 10, 2012 at 14:27
  • You are welcome @Feanne as Jon said browse php.net first that would be great.. good job.
    – GoSmash
    Commented Apr 10, 2012 at 14:32
1

There is another way of deleting multiple elements from php array.

Instead of looping through the entire array and unsetting all its keys,

you can just destroy multiple elements using unset() method like below:

Example:

$array = array("a-file","b-file","meta-file-1", "meta-file-2", "meta-file-3");

For Single key:

unset($array["meta-file-1"]);

For multiple keys in one array:

unset($array["meta-file-1"], $array["meta-file-2"], $array["meta-file-3"] ....) and so on.

see unset() in details

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.