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 Quote Link to comment 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 Quote Link to comment 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>"; }?> Quote Link to comment Share on other sites More sharing options...
suttercain Posted March 17, 2007 Author Share Posted March 17, 2007 Thank you. Quote Link to comment 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.