<?php
function balancedTagCloud(array $values, array $priorities): array
{
$low = min($priorities) - 1;
$high = max($priorities) - $low;
return array_map(
fn($v, $p) => sprintf(
'<h%1$d>%2$s</h%1$d>',
ceil(($p - $low) / $high * 6),
$v
),
$values,
$priorities
);
}
$artist = ["the roots", "michael jackson", "billy idol", "more", "and more", "and_YET_MORE"];
$counts = [
'original' => [5, 3, 9, 1, 1, 3],
'no_ones' => [5, 3, 9, 7, 7, 3],
'skewed' => [5, 3, 9, 7, 111, 3],
'all_same' => [5, 5, 5, 5, 5, 5],
'only_two' => [9, 20, 9, 20, 9, 20],
];
foreach ($counts as $count) {
echo implode("\n", balancedTagCloud($artist, $count));
echo "\n---\n";
}
- Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- <h4>the roots</h4>
<h2>michael jackson</h2>
<h6>billy idol</h6>
<h1>more</h1>
<h1>and more</h1>
<h2>and_YET_MORE</h2>
---
<h3>the roots</h3>
<h1>michael jackson</h1>
<h6>billy idol</h6>
<h5>more</h5>
<h5>and more</h5>
<h1>and_YET_MORE</h1>
---
<h1>the roots</h1>
<h1>michael jackson</h1>
<h1>billy idol</h1>
<h1>more</h1>
<h6>and more</h6>
<h1>and_YET_MORE</h1>
---
<h6>the roots</h6>
<h6>michael jackson</h6>
<h6>billy idol</h6>
<h6>more</h6>
<h6>and more</h6>
<h6>and_YET_MORE</h6>
---
<h1>the roots</h1>
<h6>michael jackson</h6>
<h1>billy idol</h1>
<h6>more</h6>
<h1>and more</h1>
<h6>and_YET_MORE</h6>
---
preferences:
101.7 ms | 403 KiB | 89 Q