If you have an array that contains a hundred different values, you may be wondering how you can check if that array contains a certain string. As you may already know, you can use the foreach() function to echo out each value contained inside the array. But let’s imagine that we simply want to execute a line of code if our array contains a certain string.
Thankfully, there is an easy way to do this.
$myString = "four";
$myArray = array("four", "eleven", "nine", "six");
Ok, now the fun part. We are going to use the in_array() function to see check if our array contains a certain string.
if(in_array($myString, $myArray)) {
echo "Found it!";
}
That’s it!