Ronald Lexburg
Ronald Lexburg

Reputation: 3

Find if one of array elements exists within a multidimensional array

There are two arrays, $banwords with multiple words and a multidimensional one $data with two fields. I'm trying to check every array[$i][0] if there is a word from the $banwords array and if there isn't to print out the second field of array[$i][1]

$banwords array:

Array
(
    [0] => nonoword
    [1] => evil
    [2] => beep
    [3] => baddy
)

$data array:


Array
(
[0] => Array
(
[0] => Example with one evil word
[1] => user1
)

[1] => Array
(
[0] => No bad word is present
[1] => user2
)

[2] => Array
(
[0] => There is a baddy word here
[1] => user3
)
)

My code so far and closest I have gotten:

<?php
for ($i=0; $i < count($data); $i++) {
    // counter to replace $i and increment it so I can go through all the array elements

    foreach ( $banwords as $banword) {
        // go through each banword and compare it to $data[$i][0]
        if ( stripos( $data[$i][0], $banword) !== FALSE ) {
            // go through each first element within $data array if a word is found then do nothing
        }
        else {
            echo $banword.'<br>';
            echo $data[$i][0] .'<br>';
            echo $data[$i][1] .'<br><br><br>';
        }
    }
}
?>

Upvotes: 0

Views: 15

Answers (0)

Related Questions