How use an array of letters as a filter for foreach loop in Php

  arrays, foreach, php

I’m trying to loop through an array while using another array as a sort of "filter":

$aa = ["b","r","i", "g", "i", "t", "t", "e"];
$bb = ["a", "e", "i", "o", "u", "r"];

foreach($aa as $b){
  if($b == $bb){
    echo "Give me an ". $b;
    echo "n";
  }else{
    echo "Give me a ". $b;
    echo "n";

  }
}

So I want it to compare the letters in $aa Against the list of letters in $bb to determine whether it should echo "an or "a" to be gramatically correct.
The only way I’ve been able to get it to work correctyly is to do this:

foreach($aa as $b){
  if($b == "a" or $b == "e" or $b == "i" or $b == "o" or $b == "u" or $b == "r"){
    echo "Give me an ". $b;
    echo "n";
  }else{
    echo "Give me a ". $b;
    echo "n";

  }
}

Source: Ask PHP

LEAVE A COMMENT