With the folowing code, I get the last if statement back as the result no matter what $types matches… I want it to match the jpg file to each word in the array…
$incoming = "Letterform, Wordmark,Someoldcrap";
$allowed = array("Letterform","Wordmark", "Pictorial", "Emblem", "Abstract");
$types = explode(",",$incoming);
$results = ""; // declare the empty string
// as was pointed out get used to writing "foreach plural as singular" and you wont get mixed up
foreach($types as $type) {
if( in_array(trim($type), $allowed)){
$results .= strtolower($type) . '.jpg for ' . $type . PHP_EOL; // concat the string
}
}
echo $results;
Your original code was flawed because you were forever overwriting any previous match.
This code concatenates a string, but could just as easily be returning an array.
if($types = 'Pictorial') {
$l = 'pictoral.jpg';
}
When you see yourself writing things over and again Pictorial, pictorial alarm bells should go off in your head and you should ask “is there a better way?” Perhaps invest some real time in grokking how arrays work.