morningside Posted September 9, 2011 Share Posted September 9, 2011 PLEASE HELP! I need help I am a new php user, and am trying to understand how to read a .txt file into an array then to retrieve the index of the array at will. <?php //echo $myfile = fopen("ArrayTestFile.txt", "r") . "File exists: " or die("File does not exist or you lack permission to open it! "); //echo "The contents of the file is " . file_get_contents("ArrayTestFile.txt") . "<br />"; $fh = fopen("ArrayTestFile.txt", 'r+') or die("Failure"); $array = array(); //creation of array $num = fgets($fh);// recursive call variable $num fgets(.txt line) for ($i = 0; $i <= 5; $i++) //for loop loads first 5 of .txt { $array["i"] = $num; //loads each line of .txt into the array at the loops index. echo "Line " . $i . " of the array is " . $array["i"]; // displays the contents of the array in a print message } ?> Thank you. MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/ Share on other sites More sharing options...
cunoodle2 Posted September 9, 2011 Share Posted September 9, 2011 Please enclose your code in the "code tags" Also I'm honestly not 100% sure what you are looking for. If the text file said.. 1) Testing 2) maximum 3) php 4) John What type of results are you looking for?? The more examples the better.. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267423 Share on other sites More sharing options...
voip03 Posted September 9, 2011 Share Posted September 9, 2011 Example ArrayTestFile.txt wohlersr; Richie Wohlers; Intermediate; Four Acesmeyersg; Greg Meyers; Novice; Four Aces readArray.php <?php $file_handle = fopen("welcome.txt", "rb"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $parts = explode(';', $line_of_text); print $parts[0] ." ". $parts[1]." ". $parts[2]." ". $parts[3]." ". $parts[4]." ". $parts[5] ; } fclose($file_handle); ?> I hop that I have answerd your query. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267427 Share on other sites More sharing options...
morningside Posted September 9, 2011 Author Share Posted September 9, 2011 I need to read a .txt file into an array. I also need to beable to call on the parts of the arrray later. I though that the best way to do this was to use a for loop and assign a $varable to each line of the .txt file that is being read. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267480 Share on other sites More sharing options...
Pikachu2000 Posted September 9, 2011 Share Posted September 9, 2011 When posting code, enclose it within the forum's . . . BBCode tags. If you want each line of the file to be one array element, you can just read it with file and well, there you have it. $array = file('text_file.txt'); Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267487 Share on other sites More sharing options...
morningside Posted September 9, 2011 Author Share Posted September 9, 2011 Thanks pika. If I were to do this though $array = file('ArrayTestFile.txt'); How would I then access each piece of the array? $array[o]; //would this access the first carracter that is read from the file; meaning 1. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267493 Share on other sites More sharing options...
Pikachu2000 Posted September 9, 2011 Share Posted September 9, 2011 $array[0] would contain the first line of the file, yes. If you wanted to see the entire structure of the array, and what is in each element you could simply just do this $array = file('ArrayTestFile.txt'); echo '<pre>'; print_r($array); echo '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267496 Share on other sites More sharing options...
morningside Posted September 9, 2011 Author Share Posted September 9, 2011 Then Should I not be able to do this? <?php $file_handle = fopen("ArrayTestFile.txt", "rb"); $line_of_text = fgets($file_handle); $parts = explode(';', $line_of_text); print $parts[0] ." ". $parts[1]." ". $parts[2]." ". $parts[3]." ". $parts[4]." ". $parts[5] ; fclose($file_handle); ?> This is the output of the above code Notice: Undefined offset: 1 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5 Notice: Undefined offset: 2 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5 Notice: Undefined offset: 3 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5 Notice: Undefined offset: 4 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5 Notice: Undefined offset: 5 in C:\Program Files (x86)\EasyPHP-5.3.8.0\www\1.php on line 5 ArrayTestFile.txtArrayTestFile.txtArrayTestFile.t xtArrayTestFile.txtArrayTestFile.txtArrayTestFile .txt MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267498 Share on other sites More sharing options...
jcbones Posted September 9, 2011 Share Posted September 9, 2011 You should be able to do this: <?php //get file into an array. $line_of_text = file('ArrayTestFile.txt'); foreach($line_of_text as $lineNumber => $line) { $line_parts = explode(';',$line); echo 'Line ' . $lineNumber . ' of the array is <br /> ' . implode('<br /> ',$line_parts); // displays the contents of the array in a print message } echo '<br /><br />You have reached the end of the file!'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267500 Share on other sites More sharing options...
Pikachu2000 Posted September 9, 2011 Share Posted September 9, 2011 Well, it actually is working to an extent. Beneath the errors, you're seeing the output from the first line of the file, which is in $parts[0]. Notice that the error messages start with the second array index, $parts[1]. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267501 Share on other sites More sharing options...
morningside Posted September 9, 2011 Author Share Posted September 9, 2011 jcbones This script isnt working. Below the script is the out put when the file is run. Line 0 of the array should be 1 sence that is the first character in the .txt file. <?php $line_of_text = file('ArrayTestFile.txt'); foreach($line_of_text as $lineNumber => $line) { $line_parts = explode(';',$line); echo 'Line ' . $lineNumber . ' of the array is <br /> ' . implode('<br /> ',$line_parts); } echo '<br /><br />You have reached the end of the file!'; ?> Line 0 of the array is ArrayTestFile.txtArrayTestFile.txtArrayTestFile.t xtArrayTestFile.txtArrayTestFile.txtArrayTestFile .txt You have reached the end of the file! MOD EDIT: code tags added. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267507 Share on other sites More sharing options...
jcbones Posted September 9, 2011 Share Posted September 9, 2011 OH, contrary to what you believe, the first line in an array is 0. Ironically, that is the first number in math as well. Post the contents of this test file. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267520 Share on other sites More sharing options...
Pikachu2000 Posted September 10, 2011 Share Posted September 10, 2011 Just in case you missed it earlier . . . When posting code, enclose it within the forum's . . . BBCode tags. Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267528 Share on other sites More sharing options...
morningside Posted September 10, 2011 Author Share Posted September 10, 2011 test file The contents are 1 2 3 4 I need to read the file into an array then access the information latter. For instance if I wanted to call the "2" again. echo $myArray[1]; // this needs to call the 2nd spot in the array. So I think that I need to use a for loop to store each line of the .txt file into an array index. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267540 Share on other sites More sharing options...
jcbones Posted September 10, 2011 Share Posted September 10, 2011 test.txt 1 2 3 4 test.php <?php //get file into an array. $line_of_text = file('test.txt'); foreach($line_of_text as $lineNumber => $line) { echo '<br />Line ' . $lineNumber . ' of the array is <br /> ' . $line; // displays the contents of the array in a print message } echo '<br /><br />You have reached the end of the file!'; ?> Output Line 0 of the array is 1 Line 1 of the array is 2 Line 2 of the array is 3 Line 3 of the array is 4 You have reached the end of the file! Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267568 Share on other sites More sharing options...
morningside Posted September 10, 2011 Author Share Posted September 10, 2011 Thanks Jcbones. But what if I wanted to call the contents of a line from the file again. Being out side of the for statment. How would I do this? Could I make the statment $line[0]; to return the 1 from the file? For example if the array was coded like this then I would be able to call elements of the array by the code echo $numbers[0]; <?php $numbers = array('10', '9', '8', '7', '6', '5', '4', '3', '2', '1'); if (file_exists("array.txt")) echo "File exists"; $fh = fopen("array.txt", 'r') or die("File Does not exist"); $line = fgets($fh); $numbers[1] = $line; $line = fgets($fh); $numbers[0] = $line; fclose($fh); echo "<br />"; echo $numbers[1]; echo "<br />"; echo $numbers[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1267663 Share on other sites More sharing options...
morningside Posted September 14, 2011 Author Share Posted September 14, 2011 <?php echo "The contents of the file is " . file_get_contents('ArrayTestFile.txt') . "<br />"; echo "<br />"; $fh = fopen("ArrayTestFile.txt", 'r+') or die("Failure"); $array = array(); //creation of array for ($j = 0; $j <= 3; $j++) //for loop loads first 5 of .txt { $num = fgets($fh);// This recursive call must be in the loop $array[$j] = $num; //loads each line of .txt into the array at the loops index. echo "Line " . $j . " of the array is " . $array[$j] . "." . "<br />"; // displays the contents of the array } ?> Quote Link to comment https://forums.phpfreaks.com/topic/246794-retrieving-variables-from-a-array-that-has-been-loaded-with-a-txt-file/#findComment-1269244 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.