1

我想从最近 50 个帖子中查找标签并打印出标签重复的数量?

到目前为止,我打印了标签。如何打印“标签重复次数”的数量。

$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.
}

我希望输出像$tag:$number<br/>...

4

1 回答 1

0

您可以像这样读取数组:

$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 />\n";
}
于 2013-06-04T00:20:57.030 回答