There is this text file with around 80,000 words, all in lower case, in alphabetical order. How do I read these words into associated arrays? I mean, words starting with 'a' go into $words, those with 'b' into $words and so on.. The array should become something like
$words[0], $words[1], $words[2],....,$words[0], $words[1], $words[2],...

Hi there,
Give this a bash, be warned though I haven't tested it yet put if you post up your text file I'd be more than happy to:

$assoc_array = array('a'=>array(),'b'=>array(),'c'=>array(),'d'=>array(),'e'=>array(),'f'=>array(),
                       'g'=>array(),'h'=>array(),'i'=>array(),'j'=>array(),'k'=>array(),'l'=>array(),
                       'm'=>array(),'n'=>array(),'o'=>array(),'p'=>array(),'q'=>array(),'r'=>array(),
                       's'=>array(),'t'=>array(),'u'=>array(),'v'=>array(),'w'=>array(),'x'=>array(),
                       'y'=>array(),'z'=>array());
  $filearr = file('myfile.txt');
  
  foreach ($filearr as $line)
  {
      $thisLine = split(" ",$line);
      foreach ($thisLine as $word)
      {
          $char = substr($word,0,1);
          array_push($assoc_array[$char],$word);
      }
  }

The text file has 100,000 + words, all separated by a "\n".
Thank you.

Hi there,
Here it is in it's adjusted form, works like a bomb. Be warned, this is quite a memory intensive little script:

$assoc_array = array();
  $filearr = file('WordList.txt');
  
  $lastChar = '';
  $i = 0;
  foreach ($filearr as $word)
  {
      $char = substr($word,0,1);
      if ($char != $lastChar)
      {
          $lastChar = $char;
          $i = 0;
      } else {
          $i++;
      }
      $assoc_array[$char][$i] = $word;
  }
  echo "<pre>";
  print_r($assoc_array);
  echo "</pre>";

Okay, thanks a lot, it works perfectly.
P.S: How can I get the amount of memory being consumed by the script?

Now, now, I can't do your entire assignment for you :P
If you get stuck on anything with php the resource to use is http://www.php.net or just Google "php" followed by your question.
Good luck, I hope you get 10/10

9/10. You gave him an array of associate arrays! ;) j/k

Now, now, I can't do your entire assignment for you :P
If you get stuck on anything with php the resource to use is http://www.php.net or just Google "php" followed by your question.
Good luck, I hope you get 10/10

A) You already did his entire assignment for him.
B) $char = substr($word,0,1); is the same as $char = $word[0]; C)

$char = substr($word,0,1);
      if ($char != $lastChar)
      {
          $lastChar = $char;
          $i = 0;
      } else {
          $i++;
      }
      $assoc_array[$char][$i] = $word;

is exactly the same as

$assoc_array[$word[0]][] = $word;

Please stop giving them answers to their homework before they show effort.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.