限制标签云中的每个循环
我有以下标签云。
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
取另一个计数器变量并在循环中递增并检查是否达到 20 打破循环
或
使用 array_slice
Take another counter variable and increment in loop and check if it is reached to 20 break the loop
OR
Use array_slice
如果您的数组尚未排序,您可以使用
arsort()
按最高结果对其进行排序。然后你可以使用array_slice()
来创建一个仅包含前 20 个数组元素的新数组:arsort
表示“关联反向排序”。这仅意味着它作用于关联数组,维护它们的键,并按其值以“反向”(即从高到低)顺序对数组进行排序。array_slice 只是对现有数组进行“切片”。在此示例中,它表示“获取
$data
数组,并返回一个包含 20 个值的新数组,从第一个值开始。为了解决您在评论中提出的有关此问题的观点,导致标签也可以按大小顺序显示,当您希望它们是随机的时,您可以使用
随机播放
获取前 20 条记录后的数组:If your array isn't already sorted, you can use
arsort()
to sort it by the highest results. Then you can usearray_slice()
to create a new array with just the first 20 array elements: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: