hashing and indexing

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View


 i am trying to count all of the words in a string, and am trying to
determine the best data structure for this.

 i would like to be able to do something like

 $arr_text = explode(" ", $text);

 then, i want to count each unique word in $arr_text

 i tried using the words as the array offsets, and of course i found
that this is illegal.

 is there a php data structure which can accomplish this for me?

 something like python's dictionary data structure would be perfect.

thank you so much for reading.


Re: hashing and indexing

Hello --

Fortunately, there is a function that was designed for this purpose:
str_word_count.  So you might do something like this:

$text = 'Some string of random words';
$words = str_word_count($text, 1);
$unique_words = array_unique($words);
$number_unique_words = count($unique_words);
print $number_unique_words;

I hope this helps.


spwpreston@gmail.com wrote:
Quoted text here. Click to load it

Re: hashing and indexing

Following on from 's message. . .

See array_count_values() in the manual.  I Believe this is exactly what  
you're looking for.

Quoted text here. Click to load it

PETER FOX Not the same since the porcelain business went down the pan
2 Tees Close, Witham, Essex.
Gravity beer in Essex  <http://www.eminent.demon.co.uk

Site Timeline