hashing and indexing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • spwpreston@gmail.com

    hashing and indexing



    hello,

    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.

    -d

  • Geoffrey

    #2
    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($w ords);
    $number_unique_ words = count($unique_w ords);
    print $number_unique_ words;

    I hope this helps.


    Geoffrey


    spwpreston@gmai l.com wrote:
    hello,
    >
    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.
    >
    -d

    Comment

    • Peter Fox

      #3
      Re: hashing and indexing

      Following on from 's message. . .

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


      >
      >
      >hello,
      >
      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.
      >
      >-d
      >
      --
      PETER FOX Not the same since the porcelain business went down the pan
      peterfox@eminen t.demon.co.uk.n ot.this.bit.no. html
      2 Tees Close, Witham, Essex.
      Gravity beer in Essex <http://www.eminent.dem on.co.uk>

      Comment

      Working...