Remove rows from a 2d array which have a column value found in a flat blacklist array

343 views Asked by At

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'],
]
4

There are 4 answers

2
AbraCadaver On BEST ANSWER

With the existing code use in_array:

function words_not_included( $w ) {
   $not_included = array('sampleword1', 'sampleword3');
   return !in_array($w['word'], $not_included);
}
4
aashnisshah On

If I understood you correctly, you have 2 arrays and you want the first array not to contain any words in the second array.

For example, if you have ['ball', 'pie', 'cat', 'dog', 'pineapple'] as the first array, and ['ball', 'cat'] as the second array, you want the output to be ['pie', 'dog', 'pineapple']

The in_array() allows you to pass in an array so that you can compare multiple values together. Based on your current code you could do something like this:

function words_not_included( $allWords, $ignoreWords ) { return !in_array( $ignoreWords, $allWords); }

0
Giwwel On

try it like this

function words_not_included($inputArray, $forbiddenWordsArray){

$returnArray = array();

//loop through the input array

for ($i = 0; $i < count($inputArray);$i++){

    foreach($inputArray[$i] as $key => $value){
        $allowWordsArray = array();
        $isAllow = false;

        //only the word element
        if($key == "word"){

            //separate the words that will be allow
            if(!in_array($value,$forbiddenWordsArray)){
                $isAllow = true;
            }
        }

        if ($isAllow === true){
            $returnArray[] = $inputArray[$i];
        }

    }
}

return $returnArray;
}


$inputArray = array();
$inputArray[] = array("occurence" => 17, "word" => "sampleword");
$inputArray[] = array("occurence" => 17, "word" => "sampleword1");
$forbiddenWords = array("sampleword");

var_dump(words_not_included($inputArray, $forbiddenWords));
0
mickmackusa On

Invert the filteration of this answer from intersection to difference.

Code: (Demo)

var_export(
    array_udiff(
        $array,
        $forbidden_words,
        fn($a, $b) =>
            ($a['word'] ?? $a)
            <=>
            ($b['word'] ?? $b)
    )
);

Because $a and $b may come from either array, try to access the value from the "word" column. If there is no "word" element, then the variable contains data from the flat array and be accessed without nominating a key.