0

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>';
        }
    }
}
?>
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • You're missing the keyword `as` in `foreach`: `foreach ($banwords as $banword)` – Barmar Dec 27 '22 at 21:49
  • @Barmar typo edited it to match it , ty – Ronald Lexburg Dec 27 '22 at 22:00
  • @Barmar the linked code on the other post, I’m not sure if it works the same with PHP. As I’ve already done the check anfter the else statement and it still displays words it shouldn’t. I’ve tried also multiple comparison === == !== for both TRUE & FALSE – Ronald Lexburg Dec 27 '22 at 23:10
  • https://pastebin.com/hyUuWZZg – Barmar Dec 27 '22 at 23:18
  • @Barmar Thank you very much, works perfectly. I now see the logic and you've taught me another lesson on arrays, as I was trying to get from start the array[0] element instead of going through it all and selecting it after. – Ronald Lexburg Dec 28 '22 at 00:06
  • Other than being written in PHP syntax, I'm not sure what's different between this and the answer in the linked question. The logic is the same. – Barmar Dec 28 '22 at 00:16

0 Answers0