DEV Community

MOORTHY
MOORTHY

Posted on

How do I find most used 10 words in php

Anyone help me how do I find most used 10 words in php

Top comments (3)

Collapse
 
llagerlof profile image
Lawrence Lagerlof • Edited
<?php
$arr = ['a', 'b', 'a', 'c', 'd', 'd', 'd'];

/*
Or this, if your input is a string
$arr = explode(' ', 'a b a c d d d');
*/

$arr_count = [];
foreach ($arr as $v) {
    $arr_count[$v] = isset($arr_count[$v]) ? $arr_count[$v] + 1 : 1;
}

arsort($arr_count);

print_r($arr_count);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
llagerlof profile image
Lawrence Lagerlof

Put the tag #help in this question, please.

Collapse
 
aboutdavid profile image
David • Edited

You could split the words based on spaces and count how many times each word is mentioned and get the top 10 words like that.