note 42090 added to language.types.array

From: Date: Mon, 03 May 2004 11:57:56 +0000
Subject: note 42090 added to language.types.array
Groups: php.notes 
Request: Send a blank email to php-notes+get-69131@lists.php.net to get a copy of this message
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 ---- Manual Page -- http://www.php.net/manual/en/language.types.array.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+42090 Delete -- http://master.php.net/manage/user-notes.php?action=delete+42090&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+42090&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#69131) next »