I have a photo list page where each photo has multiple tags (as spaced words). I'm trying to display most of the tags on the page after:
The code looks like this:
$tags1 = "red blue green yellow colors";
$tags2 = "blue green grass tree garden";
$tags3 = "pink blue colors paint sky"
addTags($tags1);
addTags($tags2);
addTags($tags3);
echo (processTags());
private $tagsArray;
public function addTags($param) {
if($param == ""){ // no tags
return;
}
$explode = explode(" ",$param);
$this->tagsArray = array_merge($this->tagsArray, $explode);
}
private function processTags(){
$processedTags = array_count_values($this->tagsArray);
arsort($processedTags);
$count = 0;
foreach($processedTags as $tag => $value) {
if(++$count>50) break; // don't print more than 50 tags
$output .= "<a href=\"/tags/$tag\">$tag</a>";
}
return $output;
}
Each photo can have dozens of tags and there can be hundreds of tags total. Is there any way to improve the code to make it more efficient? Could it be done with less code? The above code works just fine, but I'm just curious to know if there is a better way to go about it.