从一个数组中删除的项目,如果他们在其他存在数组 [英] Removing items from an array if they exist in another array
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/1728727/remove-item-from-array-if-it-exists-in-a-disallowed-words-array\">Remove如果它存在于一个不允许的词阵列从阵列项
块引用>可以说我有包含整数以下两个PHP数组:
$富=阵列(1,5,9,14,23,31,45);
$巴=阵列(14,31,36);我要删除
$ foo的
的项目,其中相同的值在$酒吧存在
所以这个过程的结果将创建一个包含
$ filteredFoo
数组:1,5,9,23,45
已经经过那里,似乎在php.net不被执行这类操作的现有功能的文档看去。所以,我通过
$ foo的
使用的foreach和迭代唯一的选择检查值$栏
在每次迭代?解决方案您可以使用
和array_diff()
:
返回一个包含了所有在array1不在任何其他阵列present条目的数组。
块引用>$ filteredFoo =和array_diff($ foo的,$栏);
Possible Duplicate:
Remove item from array if it exists in a 'disallowed words' arrayLets say I have the following two PHP arrays that contain integers:
$foo = array(1, 5, 9, 14, 23, 31, 45); $bar = array(14, 31, 36);
I want to remove the items in
$foo
where the same value exists in$bar
So the result of the process would create a
$filteredFoo
array that contains:1, 5, 9, 23, 45
Having looked through the docs on php.net there doesn't seem to be an existing function to perform this kind of action. So is my only option to use foreach and iterate through
$foo
checking values$bar
on each iteration?解决方案You can use
array_diff()
:Returns an array containing all the entries from array1 that are not present in any of the other arrays.
$filteredFoo = array_diff($foo, $bar);
这篇关于从一个数组中删除的项目,如果他们在其他存在数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!