www.webdeveloper.com
Results 1 to 6 of 6

Thread: Comparing 2 arrays

  1. #1
    Join Date
    Mar 2010
    Location
    Long Beach, CA
    Posts
    50

    Comparing 2 arrays

    I am trying to compare 2 arrays which contain zip codes using an if statement.
    If true then a flag would be set on each zip code they have in common.

    Anyone know how to write this properly?

    the first query:

    PHP Code:
    $zips $z->get_zips_in_range($userzip5_ZIPS_SORT_BY_DISTANCE_ASCtrue); 

    print_r (array_keys($zips)); 
    I have to do an array_keys because the function makes the zipcodes the keys and distance the value. I do not need the distance.

    The result is this...
    Code:
    Array ( [0] => 90805 [1] => 90847 [2] => 90712 [3] => 90711 [4] => 90848 [5] => 90221 [6] => 90807 [7] => 90723 [8] => 90714
    The second query:

    PHP Code:
    $sql "SELECT zip FROM dealers";

    $result mysql_query($sql$connection);
    if (!
    $result) {
        die(
    'Query execution problem: ' mysql_error());
    }

    while (
    $data mysql_fetch_row($result)) {

    print_r ($data);


    The result is
    Code:
    Array ( [0] => 95030 ) Array ( [0] => 95066 ) Array ( [0] => 90211 ) Array ( [0] => 60611 ) Array ( [0] => 92037 ) Array ( [0] => 33137 ) Array ( [0] => 85054 ) Array ( [0] => 48084 ) Array ( [0] => 85054 ) Array ( [0] => 90211 ) Array ( [0] => 92660 ) Array ( [0] => 91105 ) Array ( [0] => 92270 ) Array ( [0] => 94109 ) Array ( [0] => 95030 ) Array ( [0] => Highlands Ra ) Array ( [0] => 33304 ) Array ( [0] => 33137 ) Array ( [0] => 34102 ) Array ( [0] => 32750 )
    Thanks!
    Last edited by buildingbrick; 04-20-2010 at 09:08 PM.

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    Sounds like a job for array_intersect(), perhaps?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Mar 2010
    Location
    Long Beach, CA
    Posts
    50
    I was trying that but I cannot get the 2nd query to be an array?

    Do you know why its not an array?

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,617
    You would need to build the array, something like:
    PHP Code:
    $data = array();
    while(
    $row mysql_fetch_row($result))
    {
       
    $data[] = $row[0];

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Mar 2010
    Location
    Long Beach, CA
    Posts
    50
    This code goes into an indefinite loop.

  6. #6
    Join Date
    Mar 2010
    Location
    Long Beach, CA
    Posts
    50
    Nevermind..I had the print_r in the wrong place.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles