SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Looping through and extracting values from an array

    Could someone with a good understanding of arrays complete this tiny snippet of code for me? Help greatly appreciated.

    Table: list_tbl

    Column 1 | Column 2
    ---------------------
    1 | 1
    1 | 4
    2 | 3
    2 | 6

    $sql = mysql_query("SELECT column1,column2 FROM list_tbl");
    $my_array = mysql_fetch_array($sql, MYSQL_BOTH);

    //not sure how to give $something a value
    $hit=0;
    For($i=0; $i<$something; $i++)
    {
    // this is where I need help!
    // if any value in Column 2 of $my_array== $i, then if the corresponding/paired value in Column 1 ==2, then $hit=1. Otherwise $hit=0.

    }

    In the example above, $hit would be equal to 1 once $i equalled 3. In all other cases it would be 0.

  2. #2
    SitePoint Addict
    Join Date
    Dec 2002
    Location
    Gent, Belgium, Europe, World, Univers
    Posts
    397
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if ($i = $my_array[$i][1] && $my_array[$i][0] == 2) {
    $hit = 1;
    // Do Some other stuff
    }
    else {
    $hit = 0;
    // beep
    }

    PS: count($my_array) will give you the amount of rows (for your $something) ...

  3. #3
    SitePoint Enthusiast
    Join Date
    Jul 2001
    Location
    Ireland
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for that kajakske. Willie


Bookmarks

Posting Permissions

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