I'm trying to compare a comma separated string to an arrays keys like I have below, so If they match, I can mark those check boxes as checked.
I'm stuck on this. How can this be done? I can compare two arrays, but I can't think of a way to compare a comma separated string and the keys of an array. Can you help please?
$countries = array (
"US" => "United States Of America",
"GB" => "United Kingdom",
"CA" => "Canada",
"SE" => "Sweden",
"AU" => "Australia",
);
$str = 'US,CA,SE'; // This comes from a MySql table
foreach ($countries as $code=>$name) {
if(//value in comma seperated string == array key i.e US == US) {
echo '<div><input type="checkbox" name="country[]" value="'.$code.'" checked>'.$name.'</div>'.PHP_EOL;
} else {
echo '<div><input type="checkbox" name="country[]" value="'.$code.'">'.$name.'</div>'.PHP_EOL;
}
}