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],...
absk
0
Newbie Poster
Recommended Answers
Jump to PostHi 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); …
Jump to PostHi 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) …
All 7 Replies
Menster
38
Junior Poster
absk
0
Newbie Poster
Menster
38
Junior Poster
absk
0
Newbie Poster
Menster
38
Junior Poster
kireol
10
Posting Whiz
ShawnCplus
456
Code Monkey
Team Colleague
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.