jayant rawat
jayant rawat

Reputation: 305

Show First 5 value in an array that has the most duplicates values

I want to show first five array value who has most duplicate value like in tags we show the popular tags like that now what i have done till now is i am getting all tags from database as you can see below

SMS Gateway,sms service,bulk sms,bulk sms, Courier Management Software Development, Customised software solutions, domain registration, Ecommerce website designing company, matrimony website developer, seo tutorials bhopal, Web-site designing, redesigning, updationsSMS Gateway,sms service,bulk sms

and after that exploding it into array and printing it in li like below

 $tag = explode(",", $tags);

  $tagsss = array_count_values($tag);

    foreach($tagsss as $key => $value) {
      echo "<li>$key ( $value )</li>";
    }

and the result is showing like this

SMS Gateway ( 1 )sms service ( 2 )bulk sms ( 3 ) Courier Management Software Development ( 1 ) Customised software solutions ( 1 ) domain registration ( 1 ) Ecommerce website designing company ( 1 ) matrimony website developer ( 1 ) seo tutorials bhopal ( 1 ) Web-site designing ( 1 ) redesigning ( 1 ) updationsSMS Gateway ( 1 )

Now in all this entry i just want to show first five value who have the most duplicate value just like popular tags

Upvotes: 0

Views: 28

Answers (1)

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21422

Instead of using PHP function I'll get those tags from MySQL only from query like as

SELECT CONCAT( tag_name,  " ", COUNT( tag_name ) ) AS tags
FROM tags_table
GROUP BY tag_name
ORDER BY COUNT( tag_name ) DESC 
LIMIT 5

Upvotes: 1

Related Questions