SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict Robert_2006's Avatar
    Join Date
    Jun 2006
    Location
    Tarpon Springs Fl.
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array Manipulation

    I have the following array.

    PHP Code:
    Array ( [0] => Array ( [userid] => 59
                                  
    [tags] => 4
                                
    )
            [
    1] => Array ( [userid] => 1
                                
    [tags] => 8
                              
    )
            [
    2] => Array ( [userid] => 1
                                
    [tags] => 10
                              
    )
            [
    3] => Array ( [userid] => 1
                                
    [tags] => 13
                              
    )
            [
    4] => Array ( [userid] => 82
                                
    [tags] => 17
                              
    )

           ) 
    I need to get the total sum of tags for each unique userid? Do you know how to accomplish that? array_count_values() can only count STRING and INTEGER values...

  2. #2
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function countUserTags() {
        
    $out = array();
        
    $ub count$your_array );
        for ( 
    $i=0$i<$ub$i++ ) {
            
    $uid $your_array['userid'];
            if ( 
    $i ) {
                
    $out[$uid] = $your_array['tags'];
            } else {
                if ( 
    $your_array['userid'] == $uid ) {
                    
    $out['uid'] += $your_array['tags'];
                } else {
                    
    $uid $your_array['userid'];
                    
    $out[$uid] = $your_array['tags'];
                }
            }
        }
        return 
    $out;

    }
    [/PHP]
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  3. #3
    SitePoint Zealot
    Join Date
    Nov 2005
    Posts
    113
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Might also try callback with array_walk()
    PHP Code:
    $sum 0;
    function 
    tags_sum$tags$key )
    {
      global 
    $sum;
      
    $sum += $tags["tags"];
    }

    array_walk($the_array'tags_sum');
    echo (int) 
    $sum

  4. #4
    SitePoint Addict Robert_2006's Avatar
    Join Date
    Jun 2006
    Location
    Tarpon Springs Fl.
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'll give them a try in the morning and reply back, thanks guys.

  5. #5
    SitePoint Addict Robert_2006's Avatar
    Join Date
    Jun 2006
    Location
    Tarpon Springs Fl.
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    abb,

    This won't help me reduce the number of duplicate userids. I'm trying to get the sum of tags per userid.

    byron3,

    I'm still trying your suggestion but the script keeps reaching max execution time. I'm looking into seeing why. Perhaps my array is to big? I hope not as I expect it to get much ,much larger.

  6. #6
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, abb's snippet is nearly there:
    Code php:
    $sum = array();
    function tags_sum( $tags, $key )
    {
      global $sum;
      if(!isset($sum[$tags['userid']]))$sum[$tags['userid']]=0;
      $sum[$tags['userid']] += $tags["tags"];
    }
     
    array_walk($the_array, 'tags_sum');
     
    print_r($sum);
    Saul

  7. #7
    SitePoint Addict Robert_2006's Avatar
    Join Date
    Jun 2006
    Location
    Tarpon Springs Fl.
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks php_daemon. It worked like a charm. I had been trying to wrap my head around this. I saw abb's was almost there but could think of a way to sum the results in the proper key... There arrays are challenging me. Thanks again.


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
  •