I have an array:
Array
(
[0] => tom
[1] => and
[2] => jerry
)
And I also have a disallowed words array:
Array
(
[0] => and
[1] => foo
[2] => bar
)
What I need to do is remove any item in the first array that also appears in the second array, in this instance for example, key 1 would need to be removed, as 'and' is in the disallowed words array.
Now I currently have this code, which does a foreach on the disallowed words and then uses array_search to find any matches:
$arr=array('tom','and','jerry');
$disallowed_words=array('and','or','if');
foreach($disallowed_words as $key => $value) {
$arr_key=array_search($value,$array);
if($arr_key!='') {
unset($search_terms[$arr_key]);
}
}
Now I know this code sucks, what I want to know is if there is a more efficient method of removing and item from an array where it exists in another array, especially if it negates using a foreach.
Many thanks,
Ben
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…