Php 根据出现次数设置标记的样式

Php 根据出现次数设置标记的样式,php,tags,Php,Tags,假设我有一个标记数组: [ ['tag' => 'hello', 'count' => 10], ['tag' => 'house', 'count' => 8], ['tag' => 'horse', 'count' => 7], //any number of other tag ['tag' => 'alone', 'count' => 1] ] 标记的数量未知,但它们是根据出现的次数进行排序的(co

假设我有一个标记数组:

[
    ['tag' => 'hello', 'count' => 10],
    ['tag' => 'house', 'count' => 8],
    ['tag' => 'horse', 'count' => 7],
    //any number of other tag
    ['tag' => 'alone', 'count' => 1]
]
标记的数量未知,但它们是根据出现的次数进行排序的(
count
key)

现在我想对这些标记应用一些样式(字体大小),始终基于出现的次数。 例如:第一个标签有30px,最后一个标签有12px

我该怎么办?由于我不知道标签的数量,情况变得复杂了

我不需要一个最终的代码,我需要一个想法来做这件事。
谢谢

其中一种方法是-获取所有标记的
count
s之和

然后每个标签的重量将是
计数/总和(计数)


您可以为minimum
count/sum(count)
值设置minimum font size,并相应地将其放大,或为maximum
count/sum(count)
设置maximum font size,并将其缩小。

最大的算法是:

<?php
    $a = 
    [
        ['tag' => 'hello', 'count' => 10],
        ['tag' => 'house', 'count' => 8],
        ['tag' => 'horse', 'count' => 7],
        //any number of other tag
        ['tag' => 'alone', 'count' => 1]
    ];

    $max = $a[0]['count'];
    foreach($a as $n => $item){

        $max = ($item['count'] > $max) ? $item['count'] : $max;

    }
    foreach($a as $n => $item){

        $a[$n]['font_size'] = 12 + floor(($item['count'] / $max) * 18);

    }
    print_r($a);

`这是正确的解决方案

首先,要将一个值(
x
)从一个数字刻度(
minX-maxX
)更改为另一个(
minY-maxY
),以下是等式:
y=((x-minX)/(maxX-minX)*(maxY-minY))+minY

我们认为:

  • size
    是我们要应用于当前标签的大小
  • count
    是当前标记出现的次数
  • maxTag
    是出现次数最多的标记的出现次数
  • minTag
    是出现次数最少的标记的出现次数
  • maxFont
    minFont
    是我们希望使用的最大字体大小
所以:
y=((x-minX)/(maxX-minX)*(maxY+minY))+minY
size=((count minTag)/(maxTag minTag)*(maxFont minFont))+minFont

现在PHP代码非常简单:

    $maxCount = $tags[0]['count'];
    $minCount = end($tags)['count'];
    $maxFont = 60;
    $minFont = 12;

    foreach($tags as $k => $tag) {
        $tags[$k]['size'] = round((($tag['count'] - $minCount) / ($maxCount - $minCount) * ($maxFont - $minFont)) + $minFont);
    }
举个例子。这是
$tags

[
    (int) 0 => [
        'tag' => 'noombrina',
        'count' => (int) 34
    ],
    (int) 1 => [
        'tag' => 'comunedilanciano',
        'count' => (int) 22
    ],
    (int) 2 => [
        'tag' => 'lucianodalfonso',
        'count' => (int) 21
    ],
    (int) 3 => [
        'tag' => 'comunali2016',
        'count' => (int) 20
    ],
    (int) 4 => [
        'tag' => 'regioneabruzzo',
        'count' => (int) 19
    ],
    (int) 5 => [
        'tag' => 'forzaitalia',
        'count' => (int) 19
    ],
    (int) 6 => [
        'tag' => 'maurofebbo',
        'count' => (int) 17
    ],
    (int) 7 => [
        'tag' => 'mariopupillo',
        'count' => (int) 17
    ],
    (int) 8 => [
        'tag' => 'carabinieri',
        'count' => (int) 13
    ],
    (int) 9 => [
        'tag' => 'toniapaolucci',
        'count' => (int) 13
    ]
]
结果将是:

[
    (int) 0 => [
        'tag' => 'noombrina',
        'count' => (int) 34,
        'size' => (float) 60
    ],
    (int) 1 => [
        'tag' => 'comunedilanciano',
        'count' => (int) 22,
        'size' => (float) 33
    ],
    (int) 2 => [
        'tag' => 'lucianodalfonso',
        'count' => (int) 21,
        'size' => (float) 30
    ],
    (int) 3 => [
        'tag' => 'comunali2016',
        'count' => (int) 20,
        'size' => (float) 28
    ],
    (int) 4 => [
        'tag' => 'regioneabruzzo',
        'count' => (int) 19,
        'size' => (float) 26
    ],
    (int) 5 => [
        'tag' => 'forzaitalia',
        'count' => (int) 19,
        'size' => (float) 26
    ],
    (int) 6 => [
        'tag' => 'maurofebbo',
        'count' => (int) 17,
        'size' => (float) 21
    ],
    (int) 7 => [
        'tag' => 'mariopupillo',
        'count' => (int) 17,
        'size' => (float) 21
    ],
    (int) 8 => [
        'tag' => 'carabinieri',
        'count' => (int) 13,
        'size' => (float) 12
    ],
    (int) 9 => [
        'tag' => 'toniapaolucci',
        'count' => (int) 13,
        'size' => (float) 12
    ]
]

获取所有
count
s的总和,然后一些标记的权重将是
count/sum(counts)
,并相应地调整字体大小……并使用CSS类,而不是直接将样式附加到您创建的元素中。这样你以后就可以修改结果了。谢谢@u_mulder如果你写一个完整的答案,我将签署为接受。@RobbieAverill我希望大小也是可变的,所以我不能使用css类,只能使用内联样式
[
    (int) 0 => [
        'tag' => 'noombrina',
        'count' => (int) 34,
        'size' => (float) 60
    ],
    (int) 1 => [
        'tag' => 'comunedilanciano',
        'count' => (int) 22,
        'size' => (float) 33
    ],
    (int) 2 => [
        'tag' => 'lucianodalfonso',
        'count' => (int) 21,
        'size' => (float) 30
    ],
    (int) 3 => [
        'tag' => 'comunali2016',
        'count' => (int) 20,
        'size' => (float) 28
    ],
    (int) 4 => [
        'tag' => 'regioneabruzzo',
        'count' => (int) 19,
        'size' => (float) 26
    ],
    (int) 5 => [
        'tag' => 'forzaitalia',
        'count' => (int) 19,
        'size' => (float) 26
    ],
    (int) 6 => [
        'tag' => 'maurofebbo',
        'count' => (int) 17,
        'size' => (float) 21
    ],
    (int) 7 => [
        'tag' => 'mariopupillo',
        'count' => (int) 17,
        'size' => (float) 21
    ],
    (int) 8 => [
        'tag' => 'carabinieri',
        'count' => (int) 13,
        'size' => (float) 12
    ],
    (int) 9 => [
        'tag' => 'toniapaolucci',
        'count' => (int) 13,
        'size' => (float) 12
    ]
]