...

View Full Version : Finding max and min frequencies in an array after using array_count_values()



Bobafart
12-31-2007, 06:18 PM
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.



<?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?

Fumigator
12-31-2007, 06:47 PM
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 :p array_sum() will get the total for you and from there it's cake.

Bobafart
12-31-2007, 10:20 PM
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?

dumpfi
01-02-2008, 09:18 PM
Try this:

<?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 &#37;
$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

Fumigator
01-02-2008, 11:37 PM
Whoops, yeah, percentage is value / total. That answers your question.

Bobafart
01-04-2008, 03:58 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum