SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    i was wondering how I would count a specific value in an array.

    I want to get the array from:

    $result = mysql_fetch_array($query);
    foreach($result as $key => $val) {
    if($result["value"] = "1") {
    $a++;
    } elseif ($result["value"] = "2") {
    $b++;
    }

    Would this work and if not, where am i making the mistake here. I've got to count how many times the value 1, 2, 3, 4, 5, 6, 7, and 8 come up and add them to their own variable.

    Thanks
    -Peter

  2. #2
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume that you wanna count in an associative array how many times the certain values appears
    PHP Code:
    foreach($myarray as $key => $val
       { 
       if (
    $val == "1"
            { 
            
    $a++; 
            } 
       elseif (
    $val == "2"
            { 
            
    $b++; 
            } 
       } 

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    i found a way:

    Do a while mysql_fetch array.
    Put them into a array depend on the values then count it and it should work correctly!!!

    -Peter


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
  •