Results 1 to 5 of 5

Thread: Compare Array Value to All Values of Second Array

  1. #1

    Thread Starter
    Fanatic Member stickman373's Avatar
    Join Date
    Mar 2001
    Location
    MA
    Posts
    909

    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>';


  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    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>';
      } 
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Fanatic Member stickman373's Avatar
    Join Date
    Mar 2001
    Location
    MA
    Posts
    909

    Re: Compare Array Value to All Values of Second Array

    thanks both of you.

  5. #5
    New Member
    Join Date
    Oct 2005
    Posts
    1

    Question 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?
    Quote 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
  •  



Click Here to Expand Forum to Full Width