P
P
pamparumba2017-03-19 10:59:18
PHP
pamparumba, 2017-03-19 10:59:18

How to display the most popular words from an array?

php. There is a large array that I got by splitting a long string into words. Now you need to display the 20 most popular words that are most often found in the same array (string).
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
Falseclock, 2017-03-19
@Falseclock

<?php
$array = array(1, "hello", 1, "world", "hello");
$counter = array_count_values($array);
arsort($counter);

print_r(array_slice($counter,0,20));
?>

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

php.net/manual/ru/function.array-count-values.php
php.net/manual/ru/function.array-slice.php
php.net/manual/ru/function.arsort.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question