lynxus Posted December 18, 2009 Share Posted December 18, 2009 Hi Guys, How can i read 2 lines at a time from a txt file. I have a txt file with a bunch of values ie: var1 = line1 var2 = line 2 repeat var1 = line3 var2 = line 4 Etc the code i have goes through each line. What i want is it to read 2 lines, put them into 2 vars for me to use, then repeat doing the same for the next 2 lines then 2 more etc etc until it reaches the end. Any ideas? <? // Get VRFS from txt file ( should be DB ) // VRFname // VRFrd $lines = file('vrfs.txt'); foreach ($lines as $line) { Echo $line; } ?> Link to comment https://forums.phpfreaks.com/topic/185585-read-2-lines-at-a-time-from-a-txt-file/ Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2009 Share Posted December 18, 2009 this should work <?php // Get VRFS from txt file ( should be DB ) // VRFname // VRFrd $lines = file('vrfs.txt'); for ($i=0;$i<count($lines);$i+2) { echo $lines[$i]; echo $lines[$i+1]; } ?> Link to comment https://forums.phpfreaks.com/topic/185585-read-2-lines-at-a-time-from-a-txt-file/#findComment-979771 Share on other sites More sharing options...
lynxus Posted December 18, 2009 Author Share Posted December 18, 2009 Humm. It just keeps repeating the first 2 lines . ( slowly crashing the browser lol ) Looks like a endless loop for the first 2 values. Any thoughts? -Graham Link to comment https://forums.phpfreaks.com/topic/185585-read-2-lines-at-a-time-from-a-txt-file/#findComment-979775 Share on other sites More sharing options...
rajivgonsalves Posted December 18, 2009 Share Posted December 18, 2009 sorry about that the loop should have been for ($i=0;$i<count($lines);$i=$i+2) { Link to comment https://forums.phpfreaks.com/topic/185585-read-2-lines-at-a-time-from-a-txt-file/#findComment-979777 Share on other sites More sharing options...
lynxus Posted December 18, 2009 Author Share Posted December 18, 2009 Ahh man, Thats awesome! Thank you very much. -G Link to comment https://forums.phpfreaks.com/topic/185585-read-2-lines-at-a-time-from-a-txt-file/#findComment-979787 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.