This question already has an answer here:
I want to find tags from the last 50 posts and print them with the number of how many the tag was repeated?
so far I printed the tags. how can I print the number of "how many the tag was repeated".
$posts = mysql_query("SELECT * FROM posts ORDER BY id desc Limit 50");
while($fetch_tags = mysql_fetch_assoc($posts))
{
$tags_array = str_word_count($fetch_tags['tags'],1);
}
foreach($tags_array as $tag)
{
echo $tag .'<br/>'; // ex: html</br/>php</br/> ...etc.
}
i want the output be like $tag:$number<br/>
...
</div>
You can read the array like:
$posts = mysql_query("SELECT * FROM posts ORDER BY id desc Limit 50");
$results = array();
while($fetch_tags = mysql_fetch_assoc($posts))
{
$tags_array = str_word_count($fetch_tags['tags'],1);
foreach($tags_array AS $index => $tag) {
if ( !isset($results[$tag]) ) $results[$tag] = 1; // First occurrence
else $results[$tag] += 1; // Add one occurrence
}
}
// Now that we counted, print the results
foreach($results AS $tag => $number)
{
echo "{$tag} : {$number}<br />
";
}