...

View Full Version : How do you count link elements in an array?



Bobafart
12-30-2007, 07:45 AM
I have created an array. I would like to count the frequencies of each element in the array. So if "news" appears 5 times in my array I would have a count of 5.

Sample array:

print_r($tagArray);

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

how does one count the frequency for each element in the array?

(I am trying to make a tag could -- the more frequent the tag appears the larger the font)

oesxyl
12-30-2007, 08:46 AM
http://www.php.net/manual/en/function.array-count-values.php

best regards

Inigoesdr
12-30-2007, 09:21 AM
$tagArray = array('blog', 'social', 'Bluesnews', 'games', 'games', 'social', 'news', 'news', 'Fox', 'webgame', 'ajax');
$count = array();
foreach($tagArray as $tag)
$count[$tag]++;

arsort($count);
print_r($count);
Output:

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

kbluhm
12-31-2007, 12:06 AM
$tagArray = array('blog', 'social', 'Bluesnews', 'games', 'games', 'social', 'news', 'news', 'Fox', 'webgame', 'ajax');
$count = array();
foreach($tagArray as $tag)
$count[$tag]++;

arsort($count);
print_r($count);
Output:

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

Much simpler to use array_count_values()... ;)

Inigoesdr
12-31-2007, 12:59 AM
Must have been sleep typing again. :p Missed that first post too..



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum