SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot XzAzX's Avatar
    Join Date
    Apr 2002
    Location
    Holland
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Array] Count the number of entries

    Is there a way to count the number of SAME values in a array?

    Like

    PHP Code:
    $egg[0] = 1;
    $egg[1] = 1;
    $egg[2] = 2;
    $egg[3] = 3
    then a function that will return 2 if i count the number of 1's?

    thx
    _o_ OOP _o_

  2. #2
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could try this:
    PHP Code:
     function count_matches($haystack$needle) {
         
    $count 0;
         foreach (
    $haystack as $value) {
             if(
    $value == $needle)
                 
    $count++;
         }
         return 
    $count;
     } 
    I haven't tested it, let me know if it works.
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  3. #3
    SitePoint Evangelist Aska's Avatar
    Join Date
    Aug 2003
    Location
    Melbourne, Australia
    Posts
    454
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Xzazx, could array_count_values be what you are looking for?
    PHP Code:
    $votes array_count_values$egg);
    echo 
    '<p>Number of votes for 1: ' .$votes'1']. '</p>';
    echo 
    '<p>Number of votes for 2: ' .$votes'2']. '</p>';
    /* ... etc. */ 

  4. #4
    Mal Reynolds Mandibal's Avatar
    Join Date
    Aug 2003
    Location
    Columbus
    Posts
    718
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think Aska is exactly right here.
    Erh

  5. #5
    Super Ninja Monkey Travis's Avatar
    Join Date
    Dec 2001
    Location
    Sioux City, Iowa
    Posts
    691
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That almost does what he wants, but he wanted it to return a number, not an array.
    PHP Code:
     function count_matches($haystack$needle) {
         
    $temp array_count_values($haystack);
         return 
    $temp[$needle];
     } 
    Travis Watkins - Hyperactive Coder
    My Blog: Realist Anew
    Projects: Alacarte - Gnome Menu Editor

  6. #6
    SitePoint Zealot XzAzX's Avatar
    Join Date
    Apr 2002
    Location
    Holland
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
     
    reset($_SESSION['pid']);
    while(list($key,$val) = each($_SESSION['pid']))
    {
    ereg("([0-9]{1,2})\|([0-9]{1,2})", $val, $regs); 
     
    $aantal = array_count_values($_SESSION[pid]);
    $nummer = $regs[0];
    echo "waarde : $regs[0] x $aantal[$nummer]<br>";
     
    } 
     
    


    It works this simple, thx all you guys, love u
    _o_ OOP _o_


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
  •