suttercain Posted March 17, 2007 Share Posted March 17, 2007 Hey everyone, I am using the following code to extract a file into an array: <?php // set file to read $file = 'http://localhost/PHP%20Tutorials/By%20Example/examples/chapter10_examples/datebook.bck'; // read file into array $data = file($file) or die('Could not read file!'); // loop through array and print each line foreach ($data as $line) { echo $line . "<br>"; } ?> How would I assign the "lines of a file" to this code? What I mean, right now it outputs 20 lines of code and breaks them into name, street, etc. I would like it to output the numeric lines of that file. So 1. name, street, etc. 2. name, street, etc. Thanks in advance. Shannon Link to comment https://forums.phpfreaks.com/topic/43070-solved-assigning-lines-of-a-files-to-an-array/ Share on other sites More sharing options...
fert Posted March 17, 2007 Share Posted March 17, 2007 http://us3.php.net/manual/en/function.file.php Link to comment https://forums.phpfreaks.com/topic/43070-solved-assigning-lines-of-a-files-to-an-array/#findComment-209228 Share on other sites More sharing options...
kenrbnsn Posted March 17, 2007 Share Posted March 17, 2007 Just add a line count variable to your echo statement: <?php $lc = 1; foreach ($data as $line) { echo $lc++ . '. ' .$line . "<br>"; }?> Link to comment https://forums.phpfreaks.com/topic/43070-solved-assigning-lines-of-a-files-to-an-array/#findComment-209229 Share on other sites More sharing options...
suttercain Posted March 17, 2007 Author Share Posted March 17, 2007 Thank you. Link to comment https://forums.phpfreaks.com/topic/43070-solved-assigning-lines-of-a-files-to-an-array/#findComment-209232 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.