Counting duplicate tags in a string of many tags
I has been a long time since I used PHP, but what you want are multisets that are basically sets with a value -> number-of-occurrences mapping. I believe you can accomplish this in PHP via associative arrays as below:

$tags1 = "red blue green yellow colors blue red green yellow yellow";

function addTags($param) {

    // tagsArray should be a global variable
    // but put as local variable for illustration only
    $tagsArray = array();

    if($param == ""){ // no tags
        return;
    }
    $explode = explode(" ",$param);

    foreach ($explode as $value) {
       $val = 0;
       if (array_key_exists($value, $tagsArray)) {
          $val = $tagsArray[$value];          
       }

       $tagsArray[$value] = 1 + $val;
    }

    // sort the array
    arsort($tagsArray);

    // for illustration purposes only
    foreach ($tagsArray as $key =&gt; $value) {
       echo $key.':'.$value.', ';
    }
}

addTags($tags1);