|
-
Dec 11th, 2004, 03:14 PM
#1
Thread Starter
Fanatic Member
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>';
}
-
Dec 11th, 2004, 05:40 PM
#2
Re: Compare Array Value to All Values of Second Array
Use an array_find or similar function.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Dec 11th, 2004, 07:37 PM
#3
Re: Compare Array Value to All Values of Second Array
The in_array function is your friend here: 
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>'; }
-
Dec 11th, 2004, 09:21 PM
#4
Thread Starter
Fanatic Member
Re: Compare Array Value to All Values of Second Array
thanks both of you.
-
Oct 18th, 2005, 02:09 PM
#5
New Member
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?
 Originally Posted by visualAd
The in_array function is your friend here:
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>';
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|