I have the following array.
$ar = array("apple:3","banana:2","canopule:6","grapes:5","cherry:8");
How can I check to see if the following variable
$a = "grapes";
Is in the array (in this case grapes:5
) and if it is, return the matched string. I.e., "grapes : 5"
If it's not in the list (e.g., matching "strawberry"), return "strawberry : not found."
I have this:
for($i = 0; $i < count($ar); $i++) {
list($name,$count)=explode(":",$ar[$i]);
if($a==$name)
echo "$a : $count";
else
echo "$a : not found!";
}
If it matches, it works fine. But if not, I get
strawberry : not found!strawberry : not found!strawberry : not found!strawberry : not found!strawberry : not found!
It returns every false case instead of just one.