Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

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

Posts: 29
Trades: 0
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
 
 
Register now for full access!
Old 03-20-2005, 06:14 PM
0beron's Avatar
Defies a Status

Posts: 1,832
Location: Somewhere else entirely
Trades: 0
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';

Please login or register to view this content. Registration is FREE
(aka MSN handwriting for forums)
0beron is offline
Reply With Quote
View Public Profile Visit 0beron's homepage!
 
Old 03-24-2005, 12:23 PM
Average Talker

Posts: 29
Trades: 0
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 12:26 PM..
ValC is offline
Reply With Quote
View Public Profile
 
Old 03-24-2005, 02:00 PM
Rufo's Avatar
Extreme Talker

Posts: 173
Trades: 0
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++

__________________

Please login or register to view this content. Registration is FREE
Rufo is offline
Reply With Quote
View Public Profile Visit Rufo's homepage!
 
Old 03-25-2005, 03:46 PM
Average Talker

Posts: 29
Trades: 0
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, 05:16 PM
Rufo's Avatar
Extreme Talker

Posts: 173
Trades: 0
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.
__________________

Please login or register to view this content. Registration is FREE
Rufo is offline
Reply With Quote
View Public Profile Visit Rufo's homepage!
 
Reply     « Reply to Text file help
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





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



Page generated in 1.00081 seconds with 12 queries