如何按条件过滤数组
我有一个像这样的数组:
array("a" => 2, "b" => 4, "c" => 2, "d" => 5, "e" => 6, "f" => 2)
现在我想按某些条件过滤该数组,只保留值等于 2 的元素并删除值不为 2 的所有元素。
所以我的预期结果数组将是:
array("a" => 2, "c" => 2, "f" => 2)
注意:我想保留原始数组中的键。
如何使用 PHP 做到这一点?有什么内置功能吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您必须以某种方式循环遍历数组并根据条件过滤每个元素。这可以通过多种方法来完成。
循环
while
/for
/foreach
方法使用您想要的任何循环遍历数组,可能是
while
、for
或foreach
。然后只需检查您的情况并unset()
如果元素不满足条件,则将其删除,或者将满足条件的元素写入新数组。循环
条件
只需将条件放入注释
//condition
所在的循环中即可。条件可以只检查您想要的任何内容,然后您可以unset()
不满足您的条件的元素,并使用array_values()
如果需要,或者将满足条件的元素写入新数组中。array_filter()
方法另一种方法是使用
array_filter()
内置函数。它的工作原理通常与简单循环的方法几乎相同。如果您想将元素保留在数组中,则只需返回
TRUE
即可;如果您想将元素从结果数组中删除,则只需返回FALSE
即可。preg_grep()
方法preg_grep ()
与array_filter()
类似,只是它只使用正则表达式来过滤数组。因此,您可能无法用它做所有事情,因为您只能使用正则表达式作为过滤器,并且只能按值或按键使用更多代码进行过滤。另请注意,您可以传递标志
PREG_GREP_INVERT
作为第三个参数来反转结果。常见条件
用于过滤数组的常见条件有很多,其中所有条件都可以应用于数组的值和/或键。我将在这里列出其中的一些:
You somehow have to loop through your array and filter each element by your condition. This can be done with various methods.
Loops
while
/for
/foreach
methodLoop through your array with any loop you want, may it be
while
,for
orforeach
. Then simply check for your condition and eitherunset()
the elements if they don't meet your condition or write the elements, which meet the condition, into a new array.Looping
Condition
Just place your condition into the loop where the comment
//condition
is. The condition can just check for whatever you want and then you can eitherunset()
the elements which don't meet your condition, and reindex the array witharray_values()
if you want, or write the elements in a new array which meet the condition.array_filter()
methodAnother method is to use the
array_filter()
built-in function. It generally works pretty much the same as the method with a simple loop.You just need to return
TRUE
if you want to keep the element in the array andFALSE
if you want to drop the element out of the result array.preg_grep()
methodpreg_grep()
is similar toarray_filter()
just that it only uses regular expression to filter the array. So you might not be able to do everything with it, since you can only use a regular expression as filter and you can only filter by values or with some more code by keys.Also note that you can pass the flag
PREG_GREP_INVERT
as third parameter to invert the results.Common conditions
There are many common conditions used to filter an array of which all can be applied to the value and or key of the array. I will just list a few of them here:
您可以迭代键的副本,以便能够在循环中使用
unset()
:如果您的数组包含大值,则此方法的优点是内存效率 - 它们不会重复。
编辑我刚刚注意到,您实际上只需要值为 2 的键(您已经知道该值):
You can iterate on the copies of the keys to be able to use
unset()
in the loop:The advantage of this method is memory efficiency if your array contains big values - they are not duplicated.
EDIT I just noticed, that you actually only need the keys that have a value of 2 (you already know the value):
我认为最快速、可读的内置函数是: array_intersect()
代码:(Demo)
输出:
只需确保将第二个参数声明为数组,因为这是值预期类型。
现在编写 foreach 循环或使用 array_filter() 并没有什么问题,只是语法更冗长而已。
通过向第二个参数数组添加更多值,
array_intersect()
也很容易扩展(包括额外的“限定”值)。从 PHP7.4 开始,可以使用箭头函数语法。这提供了更简洁的代码以及无需
use
即可访问全局变量的能力。代码:(演示)
I think the snappiest, readable built-in function is: array_intersect()
Code: (Demo)
Output:
Just make sure you declare the 2nd parameter as an array because that is the value type expected.
Now there is nothing wrong with writing out a foreach loop, or using
array_filter()
, they just have a more verbose syntax.array_intersect()
is also very easy to extend (include additional "qualifying" values) by adding more values to the 2nd parameter array.From PHP7.4, arrow function syntax is available. This affords more concise code and the ability to access global variables without
use
.Code: (Demo)
这应该可行,但我不确定它的效率如何,因为您可能最终会复制大量数据。
This should work, but I'm not sure how efficient it is as you probably end up copying a lot of data.
这可以使用闭包来处理。以下答案的灵感来自 PHP The Right Way:
$filtered_array 的内容现在是
This can be handled using a closure. The following answer is inspired by PHP The Right Way:
Contents of $filtered_array would now be
我可能会做这样的事情:
I might do something like:
您可以执行以下操作:
或:
You can do something like:
or: