I have multiple arrays of strings and I'm trying to figure out an efficient way to loop through them for a match and if a match is found, leave the loop. For each array I'm already using a loop to check for matches. There ought to be a better way to do this than just to repeat the inner loop in code for each and every array but I can't figure out how to do it.
Here is my code. It only shows 3 arrays but I'd like to eventually expand this to many more arrays so the code will become more and more inefficient.
$query = $_Request['q'];//query from Internet
$arrayMovies = array("La Dolce Vita","East of Eden","North by Northwest");
$arrayDirectors = array("Fellini","Ray","Hitchcock");
$arrayActors = array("Giancarlo","James","Jimmy");
$match = "";
$type = "";
$phrases = $arrayMovies;
foreach($phrases as $phrase)
{
if(preg_match("/" . $phrase . "/i", $query))
{
$match = $phrase;
$type = "movie";
}
}
//repeat for next array
$phrases = $arrayDirectors;
foreach($phrases as $phrase)
{
if(preg_match("/" . $phrase . "/i", $query))
{
$match = $phrase;
$type = "director";
}
}
//repeat for next array
$phrases = $arrayActors;
foreach($phrases as $phrase)
{
if(preg_match("/" . $phrase . "/i", $query))
{
$match = $phrase;
$type = "actor";
}
}
if ($match!="") {
//DO SOMETHING
}
Is there a way to loop through the arrays and the first time we find a match leave the loop and do something with the match?