DEV Community

Discussion on: How do I find most used 10 words in php

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