<p>I have the following arrays:</p>

<pre><code>$artist = array("the roots", "michael jackson", "billy idol", "more", "and more", "and_YET_MORE");
$count = array(5, 3, 9, 1, 1, 3);
</code></pre>

<p>I want to generate a tag cloud that will have artists with a higher number in <code>$count</code> enclosed in <code>h6</code> tags and the lowest enclosed <code>h1</code> tags.</p>

[{'answer_id': 253, 'author': 'Ryan Fox', 'author_id': 55, 'author_profile': 'https://Stackoverflow.com/users/55', 'pm_score': 5, 'selected': False, 'text': '<p>Off the top of my head...</p>\n\n<pre><code>$artist = array("the roots","michael jackson","billy idol","more","and more","and_YET_MORE");\n$count = array(5,3,9,1,1,3);\n$highest = max($count);\nfor (int $x = 0; $x &lt; count($artist); $x++)\n{\n    $normalized = $count[$x] / $highest;\n    $heading = ceil($normalized * 6); // 6 heading types\n    echo "&lt;h".$heading."&gt;".$artist[$x]."&lt;/h".$heading."&gt;";\n}\n</code></pre>\n'}, {'answer_id': 274, 'author': 'Kevin', 'author_id': 40, 'author_profile': 'https://Stackoverflow.com/users/40', 'pm_score': 3, 'selected': False, 'text': '<p>@Ryan</p>\n\n<p>That\'s correct but it actually makes the tags with the least number, larger.  This code has been tested:</p>\n\n<pre><code>$artist = array("the roots","michael jackson","billy idol","more","and more","and_YET_MORE");\n$count = array(5,3,9,1,1,3);\n$highest = max($count);\nfor ($x = 0; $x &lt; count($artist); $x++) {\n    $normalized =  ($highest - $count[$x]+1) / $highest;\n    $heading = ceil($normalized * 6); // 6 heading types\n    echo "&lt;h$heading&gt;{$artist[$x]}&lt;/h$heading&gt;";\n}\n</code></pre>\n'}, {'answer_id': 676, 'author': 'Brendan', 'author_id': 199, 'author_profile': 'https://Stackoverflow.com/users/199', 'pm_score': 5, 'selected': False, 'text': '<p>Perhaps this is a little academic and off topic but <code>hX</code> tags are probably not the best choice for a tag cloud for reasons of document structure and all that sort of thing.</p>\n\n<p>Maybe <code>span</code>s or an <code>ol</code> with appropriate class attributes (plus some CSS)?</p>\n'}, {'answer_id': 87863, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': '<p>Have used this snippet for a while, credit is prism-perfect.net. Doesn\'t use H tags though</p>\n\n<pre><code>&lt;div id="tags"&gt;\n    &lt;div class="title"&gt;Popular Searches&lt;/div&gt;\n    &lt;?php\n        // Snippet taken from [prism-perfect.net]\n\n        include "/path/to/public_html/search/settings/database.php";\n        include "/path/to/public_html/search/settings/conf.php";\n\n        $query = "SELECT query AS tag, COUNT(*) AS quantity\n        FROM sphider_query_log\n        WHERE results &gt; 0\n        GROUP BY query\n        ORDER BY query ASC\n        LIMIT 10";\n\n        $result = mysql_query($query) or die(mysql_error());\n\n        while ($row = mysql_fetch_array($result)) {\n\n            $tags[$row[\'tag\']] = $row[\'quantity\'];\n        }\n\n        // change these font sizes if you will\n        $max_size = 30; // max font size in %\n        $min_size = 11; // min font size in %\n\n        // get the largest and smallest array values\n        $max_qty = max(array_values($tags));\n        $min_qty = min(array_values($tags));\n\n        // find the range of values\n        $spread = $max_qty - $min_qty;\n        if (0 == $spread) { // we don\'t want to divide by zero\n            $spread = 1;\n        }\n\n        // determine the font-size increment\n        // this is the increase per tag quantity (times used)\n        $step = ($max_size - $min_size)/($spread);\n\n        // loop through our tag array\n        foreach ($tags as $key =&gt; $value) {\n\n            // calculate CSS font-size\n            // find the $value in excess of $min_qty\n            // multiply by the font-size increment ($size)\n            // and add the $min_size set above\n            $size = $min_size + (($value - $min_qty) * $step);\n            // uncomment if you want sizes in whole %:\n            // $size = ceil($size);\n\n            // you\'ll need to put the link destination in place of the /search/search.php...\n            // (assuming your tag links to some sort of details page)\n            echo \'&lt;a href="/search/search.php?query=\'.$key.\'&amp;search=1" style="font-size: \'.$size.\'px"\';\n            // perhaps adjust this title attribute for the things that are tagged\n            echo \' title="\'.$value.\' things tagged with \'.$key.\'"\';\n            echo \'&gt;\'.$key.\'&lt;/a&gt; \';\n            // notice the space at the end of the link\n        }\n    ?&gt;\n&lt;/div&gt;\n</code></pre>\n'}, {'answer_id': 164758, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>As a helper in Rails:</p>\n\n<pre><code>def tag_cloud (strings, counts)\n    max = counts.max\n    strings.map { |a| "&lt;span style=\'font-size:#{((counts[strings.index(a)] * 4.0)/max).ceil}em\'&gt;#{a}&lt;/span&gt; "  }\nend\n</code></pre>\n\n<p>Call this from the view:</p>\n\n<pre><code>&lt;%= tag_cloud($artists, $counts) %&gt;\n</code></pre>\n\n<p>This outputs <code>&lt;span style=\'font-size:_em\'&gt;</code> elements in an array that will be converted to a string in the view to ultimately render like so:</p>\n\n<pre><code>&lt;span style=\'font-size:3em\'&gt;the roots&lt;/span&gt;\n&lt;span style=\'font-size:2em\'&gt;michael jackson&lt;/span&gt; \n&lt;span style=\'font-size:4em\'&gt;billy idol&lt;/span&gt; \n&lt;span style=\'font-size:1em\'&gt;more&lt;/span&gt; \n&lt;span style=\'font-size:1em\'&gt;and more&lt;/span&gt; \n&lt;span style=\'font-size:2em\'&gt;and_YET_MORE&lt;/span&gt; \n</code></pre>\n\n<p>It would be better to have a <code>class</code> attribute and reference the classes in a style sheet as mentioned by Brendan above.  Much better than using <code>h1-h6</code> semantically and there\'s less style baggage with a <code>&lt;span&gt;</code>.</p>\n'}, {'answer_id': 2943318, 'author': 'berkes', 'author_id': 73673, 'author_profile': 'https://Stackoverflow.com/users/73673', 'pm_score': 6, 'selected': True, 'text': '<p>You will want to add a logarithmic function to it too. (taken from tagadelic, my Drupal module to create tag clouds <a href="http://drupal.org/project/tagadelic" rel="noreferrer">http://drupal.org/project/tagadelic</a>):</p>\n\n<pre><code>db_query(\'SELECT COUNT(*) AS count, id, name FROM ... ORDER BY count DESC\');\n\n$steps = 6;\n$tags = array();\n$min = 1e9;\n$max = -1e9;\n\nwhile ($tag = db_fetch_object($result)) {\n    $tag-&gt;number_of_posts = $tag-&gt;count; #sets the amount of items a certain tag has attached to it\n    $tag-&gt;count = log($tag-&gt;count);\n    $min = min($min, $tag-&gt;count);\n    $max = max($max, $tag-&gt;count);\n    $tags[$tag-&gt;tid] = $tag;\n}\n// Note: we need to ensure the range is slightly too large to make sure even\n// the largest element is rounded down.\n$range = max(.01, $max - $min) * 1.0001;\n\nforeach ($tags as $key =&gt; $value) {\n    $tags[$key]-&gt;weight = 1 + floor($steps * ($value-&gt;count - $min) / $range);\n}\n</code></pre>\n\n<p>Then in your view or template:</p>\n\n<pre><code>foreach ($tags as $tag) {\n    $output .= "&lt;h$tag-&gt;weight&gt;$tag-&gt;name&lt;/h$tag-&gt;weight&gt;"\n}\n</code></pre>\n'}, {'answer_id': 3249162, 'author': 'danieli', 'author_id': 83382, 'author_profile': 'https://Stackoverflow.com/users/83382', 'pm_score': 2, 'selected': False, 'text': '<p>This method is for <code>SQL/PostgreSQL</code> fanatics. It does the entire job in the database, and it prints text with "slugified" link. It uses Doctrine <code>ORM</code> just for the sql call, I\'m not using objects. \nSuppose we have 10 sizes:</p>\n\n<pre><code>public function getAllForTagCloud($fontSizes = 10)\n{\n    $sql = sprintf("SELECT count(tag) as tagcount,tag,slug, \n    floor((count(*) * %d )/(select max(t) from \n        (select count(tag) as t from magazine_tag group by tag) t)::numeric(6,2)) \n         as ranking \n         from magazine_tag mt group by tag,slug", $fontSizes);\n\n    $q = Doctrine_Manager::getInstance()-&gt;getCurrentConnection();\n    return $q-&gt;execute($sql);\n}\n</code></pre>\n\n<p>then you print them with some CSS class, from .tagranking10 (the best) to .tagranking1 (the worst):</p>\n\n<pre><code>&lt;?php foreach ($allTags as $tag): ?&gt;\n    &lt;span class="&lt;?php echo \'tagrank\'.$tag[\'ranking\'] ?&gt;"&gt;\n        &lt;?php echo sprintf(\'&lt;a rel="tag" href="/search/by/tag/%s"&gt;%s&lt;/a&gt;\', \n            $tag[\'slug\'], $tag[\'tag\']\n        ); ?&gt;\n    &lt;/span&gt;\n&lt;?php endforeach; ?&gt;\n</code></pre>\n\n<p>and this is the <code>CSS</code>:</p>\n\n<pre><code>/* put your size of choice */\n.tagrank1{font-size: 0.3em;}\n.tagrank2{font-size: 0.4em;}\n.tagrank3{font-size: 0.5em;} \n/* go on till tagrank10 */\n</code></pre>\n\n<p>This method displays all tags. If you have a lot of them, you probably don\'t want your tag cloud to become a <em>tag storm</em>. In that case you would append an <code>HAVING TO</code> clause to your SQL query:</p>\n\n<pre><code>-- minimum tag count is 8 --\n\nHAVING count(tag) &gt; 7\n</code></pre>\n\n<p>That\'s all</p>\n'}, {'answer_id': 41906389, 'author': 'Durgaprasad', 'author_id': 3391693, 'author_profile': 'https://Stackoverflow.com/users/3391693', 'pm_score': 2, 'selected': False, 'text': '<p>I know it\'s a very old post, still I\'m posting my view as it may help someone in future.</p>\n<p>Here is the tagcloud I used in my website:\n<a href="http://www.vbausefulcodes.in/" rel="nofollow noreferrer">http://www.vbausefulcodes.in/</a></p>\n<pre><code>&lt;?php\n$input= array(&quot;vba&quot;,&quot;macros&quot;,&quot;excel&quot;,&quot;outlook&quot;,&quot;powerpoint&quot;,&quot;access&quot;,&quot;database&quot;,&quot;interview questions&quot;,&quot;sendkeys&quot;,&quot;word&quot;,&quot;excel projects&quot;,&quot;visual basic projects&quot;,&quot;excel vba&quot;,&quot;macro&quot;,&quot;excel visual basic&quot;,&quot;tutorial&quot;,&quot;programming&quot;,&quot;learn macros&quot;,&quot;vba examples&quot;);\n\n$rand_tags = array_rand($input, 5);\nfor ($x = 0; $x &lt;= 4; $x++) {\n    $size = rand ( 1 , 4 );\n    echo &quot;&lt;font size=\'$size\'&gt;&quot; . $input[$rand_tags[$x]] . &quot; &quot; . &quot;&lt;/font&gt;&quot;;\n}\n\necho &quot;&lt;br&gt;&quot;;\n$rand_tags = array_rand($input, 7);\nfor ($x = 0; $x &lt;= 6; $x++) {\n    $size = rand ( 1 , 4 );\n    echo &quot;&lt;font size=\'$size\'&gt;&quot; . $input[$rand_tags[$x]] . &quot; &quot; . &quot;&lt;/font&gt;&quot;;\n}\n\necho &quot;&lt;br&gt;&quot;;\n$rand_tags = array_rand($input, 5);\nfor ($x = 0; $x &lt;= 4; $x++) {\n    $size = rand ( 1 , 4 );\n    echo &quot;&lt;font size=\'$size\'&gt;&quot; . $input[$rand_tags[$x]] . &quot; &quot; . &quot;&lt;/font&gt;&quot;;\n}\n?&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/227', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/146637/']