note 42090 deleted from language.types.array by tomsommer
| From: | tomsommer@php.net | Date: | Mon, 03 May 2004 15:56:14 +0000 |
| Subject: | note 42090 deleted from language.types.array by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-69153@lists.php.net to get a copy of this message | ||
Note Submitter: ng4rrjanbiah@rediffmail.com
----
Here is a one-pass loop to find out the frequency of words in a text. It uses hash array for this
logic.
<?php
$words = 'I love PHP coz PHP rocks'; //"PHP" occurs twice
$words_arr = explode(' ', $words); //get the words into an array
//the tricky one-pass loop to find frequency of words using hash array
foreach($words_arr as $value)
$word_count[$value] = isset($word_count[$value]) ? ($word_count[$value] + 1) : 1;
//now, simple demonstration of the result in $word_count[]...
echo 'Frequency of words: '."\n";
foreach($word_count as $key=>$value)
echo $key.' : '.$value."\n";
echo 'Duplicates removed:'."\n";
foreach($word_count as $key=>$value)
echo $key."\n";
?>
HTH,
R. Rajesh Jeba Anbiah