Compare Array Value to All Values of Second Array
Alright I have the array $selectedcounties and I have the array $countryrow which is an array based off MySQL results. I want to compare each $countyrow array value to all of the $selectedcounties array values to see if they have a match. If they do the $selected variable is set when i'm building a dropdown list.
The code only works part way. Any better way to do this?
PHP Code:
$selectedcounties = explode(",",$notify['counties']);
while ($countyrow = mysql_fetch_array($county))
{
$selected = "";
foreach($selectedcounties as $value)
{
if($value == $countyrow['county'])
{
$selected = " selected";
}
}
$countydata .= '<option value="'.$countyrow['county'].'"'.$selected.'>'.ucwords(strtolower($countyrow['county'])).'</option>';
}
Re: Compare Array Value to All Values of Second Array
Use an array_find or similar function.
Re: Compare Array Value to All Values of Second Array
The in_array function is your friend here: :p
PHP Code:
$selectedcounties = explode(",",$notify['counties']);
while ($countyrow = mysql_fetch_array($county))
{
in_array($countryrow['county'])?$selected = ' selected="selected"':$selected = '';
$countydata .= '<option value="'.$countyrow['county'].'"'.$selected.'>'.ucwords(strtolower($countyrow['county'])).'</option>';
}
Re: Compare Array Value to All Values of Second Array
thanks both of you. :thumb:
Re: Compare Array Value to All Values of Second Array
Can this be done in visual basic 6 or is there an easier way to do this? :confused:
Quote:
Originally Posted by visualAd
The
in_array function is your friend here: :p
PHP Code:
$selectedcounties = explode(",",$notify['counties']);
while ($countyrow = mysql_fetch_array($county))
{
in_array($countryrow['county'])?$selected = ' selected="selected"':$selected = '';
$countydata .= '<option value="'.$countyrow['county'].'"'.$selected.'>'.ucwords(strtolower($countyrow['county'])).'</option>';
}