限制标签云中的每个循环

发布于 2024-11-01 13:34:12 字数 1348 浏览 1 评论 0原文

我有以下标签云。

$rows = $db->loadObjectList();
foreach ($rows as $row)
{
$strAllTags .= $row->caption . ","; 
}

// Store frequency of words in an array
$freqData = array(); 

// Get individual words and build a frequency table
foreach( explode(",", $strAllTags) as $word )
{
// For each word found in the frequency table, increment its value by one
array_key_exists( trim($word), $freqData ) ? $freqData[ trim($word) ]++ : $freqData[ trim($word) ] = 0;
}


function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 32 )
{
$minimumCount = min( array_values( $data ) );
$maximumCount = max( array_values( $data ) );
$spread       = $maximumCount - $minimumCount;
$cloudHTML    = '';
$cloudTags    = array();

$spread = 55;

foreach( $data as $tag => $count )
{
            if ($count > 4)
            {
    $size = $minFontSize + ( $count - $minimumCount ) 
        * ( $maxFontSize - $minFontSize ) / $spread;
    $cloudTags[] = '[[a style="font-size: ' . floor( $size ) . 'px' 
    . '" class="tag_cloud" href="/home?func=search&searchfield=' . $tag 
    .  '"]]' 
    . htmlspecialchars( stripslashes( $tag ) ) . '[[/a]]';
           }
}

return join( "\n", $cloudTags ) . "\n";
}   
echo getCloud($freqData);
?>

它工作正常,我只需要将其限制为前 20 个结果,关于如何做到最好有什么想法吗?

谢谢,如果您需要查看其余代码,请告诉我。

I have the following tag cloud.

$rows = $db->loadObjectList();
foreach ($rows as $row)
{
$strAllTags .= $row->caption . ","; 
}

// Store frequency of words in an array
$freqData = array(); 

// Get individual words and build a frequency table
foreach( explode(",", $strAllTags) as $word )
{
// For each word found in the frequency table, increment its value by one
array_key_exists( trim($word), $freqData ) ? $freqData[ trim($word) ]++ : $freqData[ trim($word) ] = 0;
}


function getCloud( $data = array(), $minFontSize = 12, $maxFontSize = 32 )
{
$minimumCount = min( array_values( $data ) );
$maximumCount = max( array_values( $data ) );
$spread       = $maximumCount - $minimumCount;
$cloudHTML    = '';
$cloudTags    = array();

$spread = 55;

foreach( $data as $tag => $count )
{
            if ($count > 4)
            {
    $size = $minFontSize + ( $count - $minimumCount ) 
        * ( $maxFontSize - $minFontSize ) / $spread;
    $cloudTags[] = '[[a style="font-size: ' . floor( $size ) . 'px' 
    . '" class="tag_cloud" href="/home?func=search&searchfield=' . $tag 
    .  '"]]' 
    . htmlspecialchars( stripslashes( $tag ) ) . '[[/a]]';
           }
}

return join( "\n", $cloudTags ) . "\n";
}   
echo getCloud($freqData);
?>

It works fine, I simply need to limit it to the top 20 results, any ideas as to how to do this best?

Thanks, let me know if you need to see the rest of the code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

打小就很酷 2024-11-08 13:34:12

取另一个计数器变量并在循环中递增并检查是否达到 20 打破循环

使用 array_slice

$data = array_slice($data, 0, 20);

foreach( $data as $tag => $count )
{
.....

Take another counter variable and increment in loop and check if it is reached to 20 break the loop

OR

Use array_slice

$data = array_slice($data, 0, 20);

foreach( $data as $tag => $count )
{
.....
时光与爱终年不遇 2024-11-08 13:34:12

如果您的数组尚未排序,您可以使用 arsort() 按最高结果对其进行排序。然后你可以使用 array_slice() 来创建一个仅包含前 20 个数组元素的新数组:

arsort($data);
$data = array_slice($data, 0, 20);

arsort 表示“关联反向排序”。这仅意味着它作用于关联数组,维护它们的键,并按其值以“反向”(即从高到低)顺序对数组进行排序。

array_slice 只是对现有数组进行“切片”。在此示例中,它表示“获取 $data 数组,并返回一个包含 20 个值的新数组,从第一个值开始。

为了解决您在评论中提出的有关此问题的观点,导致标签也可以按大小顺序显示,当您希望它们是随机的时,您可以使用 随机播放获取前 20 条记录后的数组:

arsort($data);
$data = array_slice($data, 0, 20);
shuffle($data);

If your array isn't already sorted, you can use arsort() to sort it by the highest results. Then you can use array_slice() to create a new array with just the first 20 array elements:

arsort($data);
$data = array_slice($data, 0, 20);

arsort means "associative reverse sort". This just means that it acts on associative arrays, maintaining their keys, and sorts the array in "reverse" (i.e. high to low) order by it's values.

array_slice just "slices" up an existing array. In this example, it says "take the $data array, and return a new array containing 20 of its values, starting from the first one.

To address the point you made in the comments about this causing the tags to also be displayed in order by size, when you want them to be random. You can accomplish this by using shuffle on the array after you grab the top 20 records:

arsort($data);
$data = array_slice($data, 0, 20);
shuffle($data);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文