I have an array that I need to filter. I'd like to filter with an array of blacklisted words to have an new array without those banned 'words'.
[
['occurence' => 17, 'word' => 'sampleword'],
['occurence' => 14, 'word' => 'sampleword1'],
['occurence' => 14, 'word' => 'sampleword2'],
['occurence' => 14, 'word' => 'sampleword3'],
]
I have one function which works pretty well, but it only filters by one 'word'.
function words_not_included( $w ) {
$not_included = 'sampleword1';
return $w['word'] != $not_included;
}
then I applied
$new_array = array_filter($old_array, "words_not_included");
So it works with one word.
How can I have an array of forbidden 'words' like:
$forbidden_words = ['sampleword1', 'sampleword3'];
and then filter with them and output an new array like this:
[
['occurence' => 17, 'word' => 'sampleword'],
['occurence' => 14, 'word' => 'sampleword2'],
]
With the existing code use in_array
:
function words_not_included( $w ) {
$not_included = array('sampleword1', 'sampleword3');
return !in_array($w['word'], $not_included);
}