Create Tag Cloud from Array in PHP

Tag cloud is a popular representation in web now a days. Most of the time we use plugin or something like that to show tag cloud. If you want to create a tag cloud from an ARRAY in PHP, follow me..

Let we want to create tag cloud for ‘fruits’ of our site. and assume we have some fruits named  ‘Apple’, ‘Banana’, ‘Mango’, ‘Orange’ etc.. where the amount of this fruit is 4, 12, 6, 17 correspondingly. and we want when we click on an item in the cloud it will go to different links like, “http://mysite.com/mango”, “http://mysite.com/banana” etc..

So we need to build an array like,

<?php
$fruits[0] = array ('name' => 'Apple',  'amount' => 4, 'link' => "http://mysite.com/apple");
$fruits[1] = array ('name' => 'Banana',  'amount' => 12, 'link' => "http://mysite.com/banana");
$fruits[2] = array ('name' => 'Mango',  'amount' => 17, 'link' => "http://mysite.com/mango");
?>
and so on..

Now add this function to your code,

<?php
function themeTagCloud($tagarray)
{
  $maxsize = 30;
  $minsize = 10;

  $tagstmp = array();
  foreach ($tagarray as $key => $value) {
    $tagstmp[$value['name']] = $value['amount'];
  }
  $maxqty = @max(array_values($tagstmp));
  $minqty = @min(array_values($tagstmp));

  $spread = $maxqty - $minqty;
  if ($spread == 0) $spread = 1;
  $step = ($maxsize - $minsize) / ($spread);

  $output = '';
  foreach ($tagarray as $key => $value)  {
    $size = round($minsize + (($value['amount'] - $minqty) * $step));
    $output .= '<a href="'.$value['link'].'" style="font-size:'.$size.'px;" title="'.$value['amount'].' times">'.$value['name'].'</a>'; // For normal PHP
  }
  return $output;
}
?>

now call this function from where you wnat to show your array as a tag cloud.

<?php print themeTagCloud($fruits); ?>

Enough! :)

—————————————————————————————————————-

If you have a plain array like this,

<?php $fruits = array('banana', 'banana', 'apple', 'banana', 'kiwi', 'apple', 'banana'); ?>

Then Use this function

<?php
function themeTagCloud($tags = array()) {
  $maxsize = 30;
  $minsize = 10;

  $tagstmp = array_count_values($tags);
  $maxqty = @max(array_values($tagstmp));
  $minqty = @min(array_values($tagstmp));

  $spread = $maxqty - $minqty;
  if ($spread == 0) $spread = 1;
  $step = ($maxsize - $minsize) / ($spread);

  $output = '';
  foreach ($tagstmp as $name => $amount) {
    $size = round($minsize + (($amount - $minqty) * $step));
    $output .= '<a style=\"font-size:'.$size.'px;\" title=\"'.$amount.' times\">'.$name.'</a>';
  }
  return $output;
}
?>

try it,

<?php echo themeTagCloud($fruits); ?>
Advertisements

3 thoughts on “Create Tag Cloud from Array in PHP

  1. Well, thanks for that approach! But I ask myself what to do one step before that: I get the results from a DB query in an array like this:

    banana
    banana
    apple
    banana
    kiwi
    apple
    banana

    how do i put these in a format that could be used by your function?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s