从PHP数组中删除多个元素的有效方法

时间:2023-02-01 21:42:31

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:

目前我正在考虑几种方法来做到这一点。一种方法,使用array_filter和自定义函数:

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:

另一种方法,使用array_keys并取消设置:

$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?

也许有一种方法可以在array_search函数中有多个搜索值?

2 个解决方案

#1


22  

You should simply use array_diff:

你应该只使用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.

关于PHP的一个坏处是它有数以万计的函数来执行特定的小事情,但有时也会变得方便。

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.

当你遇到这样的情况时(你找到了一个找到相关功能后的解决方案,但你不确定是否有更好的东西),最好在php.net上闲暇浏览功能列表边栏。只需阅读功能名称即可获得丰厚的回报。

#2


1  

Use array_diff()

使用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.

将返回一个数组,其中包含$ exclude_file_array中不在$ files中的所有元素。

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

Its better than your own function and loops.

它比你自己的功能和循环更好。

#1


22  

You should simply use array_diff:

你应该只使用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.

关于PHP的一个坏处是它有数以万计的函数来执行特定的小事情,但有时也会变得方便。

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.

当你遇到这样的情况时(你找到了一个找到相关功能后的解决方案,但你不确定是否有更好的东西),最好在php.net上闲暇浏览功能列表边栏。只需阅读功能名称即可获得丰厚的回报。

#2


1  

Use array_diff()

使用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.

将返回一个数组,其中包含$ exclude_file_array中不在$ files中的所有元素。

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

Its better than your own function and loops.

它比你自己的功能和循环更好。