Re: [PHP] Creating an array with a file
Scott, > I am seeking some thoughts on if this is the way I should tackle this > problem. I have two files, both of them tab delimited text files that I > need to combine and then output a new file. > > My idea was to put both files in an array, calling the fields I need, > i.e. $field[0], $field[1] and so on. I need to loop through the entire > file and output on each loop. > > My concern is the overhead by creating the array's. Any thoughts. A justifiable concern. You don't mention HOW the files are to be combined, so it is reasonable to assume that there is no criteria. It is also assumed that each file is 'complete' in its format (else you might need to insert something specifically 'between' the contents of the two original files to make the data 'flow' as a single file). There is no need to use arrays/cycle through the data twice, at all - such would be an 'overhead'. Take a look at http://www.php.net/manual/en/ref.filesystem.php. Open fileA to write-append Open fileB Read chunk of fileB Write chunk to fileA until have dealt with entire file Close fileA Close fileB The "chunk" bit could be binary and it could be done all in one read, depending upon expected file sizes. If by "new file" you mean that it must be a third file and have a different name to the two input files, then you will need to modify the above, to open a fileC for output, and perform one filecopy for each of files A&B. Regards, =dn -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Creating an array with a file
I am seeking some thoughts on if this is the way I should tackle this problem. I have two files, both of them tab delimited text files that I need to combine and then output a new file. My idea was to put both files in an array, calling the fields I need, i.e. $field[0], $field[1] and so on. I need to loop through the entire file and output on each loop. My concern is the overhead by creating the array's. Any thoughts. Thanks, -Scott -Original Message- From: Paul Roberts [mailto:[EMAIL PROTECTED]] Sent: Sunday, February 10, 2002 2:35 PM To: Scott Subject: Re: [PHP] Creating an array with a file try this, you need to get the line then explode that, fopen just returns a file handle. $file = fopen("oldfile.txt", "r"); while (!feof ($fd)) { $lines = fgets($file, 4096) $newfields = explode("\t", $lines); echo $newfields[0].""; } Paul Roberts [EMAIL PROTECTED] - Original Message - From: "Scott" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, February 11, 2002 5:32 PM Subject: [PHP] Creating an array with a file > Hi All, > > I am trying to create an array from a text file that I will process and > rewrite in a new format. My question is, is it possible to read the > file > in one line at a time and then separate the tab-delimited fields and > finally rewrite the new file? > > The fields are tab delimited and here is what I have attempted in code: > > $lines = fopen("oldfile.txt", "r"); > $newfields = explode("\t", $lines); > echo $newquotes[0]; > > I get a Resource ID #1 when I echo the line. > > Thanks, > > -Scott > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Creating an array with a file
You need to actually read the file in order for this to work. You're just opening it and then printing the file handle... Bogdan Scott wrote: > Hi All, > > I am trying to create an array from a text file that I will process and > rewrite in a new format. My question is, is it possible to read the > file > in one line at a time and then separate the tab-delimited fields and > finally rewrite the new file? > > The fields are tab delimited and here is what I have attempted in code: > > $lines = fopen("oldfile.txt", "r"); > $newfields = explode("\t", $lines); > echo $newquotes[0]; > > I get a Resource ID #1 when I echo the line. > > Thanks, > > -Scott > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Creating an array with a file--Fixed
Nevermind, I was able to figure it out using: $myfile = file("oldfile.txt"); for ($s=0; $s<=count($myfile)-1; $s++) { $fields = split("\t",$myfile[$s]); print("$fields[1]\n"); } -Original Message- From: Scott [mailto:[EMAIL PROTECTED]] Sent: Monday, February 11, 2002 12:33 PM To: [EMAIL PROTECTED] Subject: [PHP] Creating an array with a file Hi All, I am trying to create an array from a text file that I will process and rewrite in a new format. My question is, is it possible to read the file in one line at a time and then separate the tab-delimited fields and finally rewrite the new file? The fields are tab delimited and here is what I have attempted in code: $lines = fopen("oldfile.txt", "r"); $newfields = explode("\t", $lines); echo $newquotes[0]; I get a Resource ID #1 when I echo the line. Thanks, -Scott -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Creating an array with a file
Hi All, I am trying to create an array from a text file that I will process and rewrite in a new format. My question is, is it possible to read the file in one line at a time and then separate the tab-delimited fields and finally rewrite the new file? The fields are tab delimited and here is what I have attempted in code: $lines = fopen("oldfile.txt", "r"); $newfields = explode("\t", $lines); echo $newquotes[0]; I get a Resource ID #1 when I echo the line. Thanks, -Scott -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php