You need a dictionary.
Interestingly, PHP arrays can function as dictionaries. I found this out now, researching to answer the question. So let's get started:
$dicionario = array();
The logic is this: make a dictionary where keys are words, and value is the number of occurrences.
Since each record has multiple words separated by commas, let's start by extracting words from each record. In most languages we call it split ( split
), but since PHP is a terrorist thing in it we use the explode
function:
$palavras = explode(",", $registro);
And then we include the words in the dictionary as follows: if the key does not exist, it is created with a value of zero. Then, regardless of existing or not, we increase its value.
foreach ($palavras as $chave) {
if (!$dicionario[$chave]) {
$dicionario[$chave] = 0;
}
$dicionario[$chave]++;
}
Note that we have to blast and add to the dictionary once for each record.
Finally, we need to get the records with the ten largest values. The algorithm below removes the ten largest values from the dictionary while including them in an orderly manner in another dictionary.
$dezMaiores = array();
for ($i = 1; $i <= 10; $i++) {
$maiorValor = 0;
$maiorChave = "";
foreach ($dicionario as $chave => $valor) {
if ($valor > $maiorValor) {
$maiorValor = $valor;
$maiorChave = $chave;
}
}
$dezMaiores[$i] = $maiorChave;
unset($dicionario[$maiorChave]);
}
Now you can use the ten most used expressions on your system:)