Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Finding max and min frequencies in an array after using array_count_values()

    I have an array and I used array_count_values() to count the common elements in an array. I am not trying to assign a weight to each element in the array based on their occurences.. the more frequent the element in the array the higher the weight. I tried using min() and max() on the array but it doesn't work since it returns the string element in the array. Not sure how to do this.

    Code:
    <?php
    $sqlCountBkmarks = "SELECT bookmarkTag FROM abookmarkTags WHERE userid = {$_SESSION[userid]} AND  bookmarkid = $id";
    $resultCountBkmarks = mysql_query($sqlCountBkmarks);
    while($rowBookmark = mysql_fetch_assoc($resultCountBkmarks)){
         $tagArray[] = $rowBookmark['bookmarkTag']; 
    }
    print_r(array_count_values($tagArray)); 
    $maxsize = 150;
    $minsize = 100;
    									
    $max = max($tagArray);
    $min = min($tagArray);
    echo 'max: '.$max.' min: '.$min;
    $range = $max - $min;
    									
    $step = ($maxsize=$minsize)/$range;
    									
    foreach($tagArray as $word=>$strength)	{
    	$size = $minsize + ($step*$strength);
    	echo '<span id="bookmarktags" style="font-size:'.$size.'%;"> '.$word.' </span>';
    }
    ?>
    my array for this example is:

    Array ( [0] => blog [1] => social [2] => Bluesnews [3] => games [4] => games [5] => social [6] => news [7] => news [8] => Fox [9] => webgame [10] => ajax )

    print_r(array_count_values($tagArray)); gives:

    Array ( [webgame] => 1 [social] => 2 [news] => 2 [games] => 2 [blog] => 1 [ajax] => 1 [Fox] => 1 [Bluesnews] => 1 )

    Now that I have counted the frequencies.. how do I find the max and min (most and least frequent values) and then assign font-size weight to each? the more frequent the larger the font?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Seems you've done the hard part already. Now just find the percentage of each element and determine a scale for the font (1-20% is font size small, 30%-50% is font size medium, etc). Percentage is total / value array_sum() will get the total for you and from there it's cake.

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    Don't you mean: Percentage is value / total?

    perhaps I didn't ask the question properly -- how do I get the value for each element?

  • #4
    Regular Coder
    Join Date
    Jun 2004
    Posts
    565
    Thanks
    0
    Thanked 18 Times in 18 Posts
    Try this:
    PHP Code:
    <?php
    /**
      signature
        array getMinMax( array )

      returns an associative array with:
        'min' -> smallest value in the given array
        'max' -> greatest value in the given array

      if the array is empty both fields are NULL
    */
    function getMinMax($array)
    {
        
    reset($array);
        if(
    FALSE === key($array))
        {
            return array(
    'min' => NULL'max' => NULL);
        }
        
        
    $min $max current($array);
        
    $val next($array);
        
        while(
    NULL !== key($array))
        {
            if(
    $val $max)
            {
                
    $max $val;
            }
            elseif(
    $val $min)
            {
                
    $min $val;
            }
            
    $val next($array);
        }
        return array(
    'min' => $min'max' => $max);
    }

    $sqlCountBkmarks "SELECT bookmarkTag FROM abookmarkTags WHERE userid = {$_SESSION[userid]} AND  bookmarkid = $id";
    $resultCountBkmarks mysql_query($sqlCountBkmarks);
    while(
    $rowBookmark mysql_fetch_assoc($resultCountBkmarks)){
         
    $tagArray[] = $rowBookmark['bookmarkTag']; 
    }

    $tagCounts array_count_values($tagArray); // count the frequency of each word
    $minMax getMinMax($tagCounts); // get minimum and maximum frequency

    $maxsize 150// max fontsize in %
    $minsize 100// min fontsize in %

    $range $minMax['max'] - $minMax['min'];
    $sizePerStep = ($maxsize $minsize) / $range// percentual fontsize increase per occurence of a certain word in the $tagArray

    foreach($tagCounts as $word => $count)
    {
        
    $size $minsize + ($sizePerStep * ($count $minMax['min'])); // fontsize of the current word
        
    echo '<span id="bookmarktags" style="font-size:'.$size.'%;"> '.$word.' </span>';
    }
    ?>
    dumpfi
    "Failure is not an option. It comes bundled with the software."
    ....../)/)..(\__/).(\(\................../)_/)......
    .....(-.-).(='.'=).(-.-)................(o.O)...../<)
    ....(.).(.)("}_("}(.)(.)...............(.)_(.))Ż/.
    ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ
    Little did the bunnies suspect that one of them was a psychotic mass murderer with a 6 ft. axe.

  • Users who have thanked dumpfi for this post:

    Bobafart (01-04-2008)

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Whoops, yeah, percentage is value / total. That answers your question.

  • Users who have thanked Fumigator for this post:

    Bobafart (01-04-2008)

  • #6
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    omg dumfi, that was just beautiful

    I am going to thank all of your recent posts in every thread I can find


    thanks again gents


  •  

    Posting Permissions

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