Reply
Text file help
Old 03-19-2005, 06:30 PM Text file help
Average Talker

Posts: 29
how could i make words (seperated by a space) in a text file on a single line part of an array?

i mean as in if the text file contains the lines:
"
one two three four
five six seven
"

how could i make
"one, two, three, four" the array $line1 (so $line1[0] = "one" and $line1[1] = "two" etc)
and
"five, six, seven" the array $line2
and so on.

thanks alot.
__________________
-ValC
ValC is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
Old 03-20-2005, 07:14 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
You can either do this so you end up with lots of arrays called line1 line2 etc. or you could have one array, (call it thefile[] )with two dimensions. In your above example thefile[1][0] = "one" thefile[1][1] = "two" and thefile[2][0] = "five"

would that be more useful, or do you want named arrays like line1 line2 line3 etc?

Both are possible,
__________________
UPDATE 0beron SET talkupation = talkupation + lots WHERE post = 'helpful';
Scribble Pad MOD for phpBB (aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-24-2005, 01:23 PM
Average Talker

Posts: 29
the way you suggest sounds like it'd be best, how could that be set up?

thanks
__________________
-ValC

Last edited by ValC : 03-24-2005 at 01:26 PM.
ValC is offline
Reply With Quote
View Public Profile
 
Old 03-24-2005, 03:00 PM
Rufo's Avatar
Extreme Talker

Posts: 173
Try this.

PHP Code:
$bigarray=new array();

$lines=file('file.txt');
$d1=0;
foreach(
$lines as $line) {
   
$bigarray[$d1]=new array();
   
$words=explode(' ',$line);
   
$d2=0;
   foreach(
$words as $word) {
      
$bigarray[$d1][$d2]=$word;
      
$d2++;
   }
   
$d1++

__________________
mp3blog.us
Rufo is offline
Reply With Quote
View Public Profile Visit Rufo's homepage!
 
Old 03-25-2005, 04:46 PM
Average Talker

Posts: 29
cool thats excellent. You reckon u could tell me how it works? I like to understand everything i use so i know how to use it next time

Thanks alot
__________________
-ValC
ValC is offline
Reply With Quote
View Public Profile
 
Old 03-26-2005, 06:16 PM
Rufo's Avatar
Extreme Talker

Posts: 173
PHP Code:
$lines=file('file.txt'); 
This loads the file into $lines, one line of text per item in the array. (info here)

PHP Code:
foreach($lines as $line) { 
For each iteration of this loop, the next line is loaded into $line
$d1 is for keeping track of $bigarray's subscripts.

PHP Code:
$bigarray[$d1]=new array(); 
Makes $bigarray[$d1] an array itself, so that it can hold all the words.

PHP Code:
$words=explode(' ',$line); 
Splits up the current line into the $words array (using a space as the delimeter).

PHP Code:
foreach($words as $word) { 
For each iteration of this loop, the next word is loaded into $word
$d2 is for keeping track of $bigarray[$d1]'s subscripts.

PHP Code:
$bigarray[$d1][$d2]=$word
Put each word in the appropriate part of $bigarray.

PHP Code:
$d2++; 
Increment $d2 counter ready for the next word.

PHP Code:
$d1++ 
Increment $d1 counter ready for the next line.
__________________
mp3blog.us
Rufo is offline
Reply With Quote
View Public Profile Visit Rufo's homepage!
 
Reply     « Reply to Text file help
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Webmaster Resources Marketplace:
Software Development Company | Webhosting.UK.com | Text Link Brokers 


   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.13750 seconds with 12 queries