Counting Print Statement Help

If I have a text file that contains 100 records. Each of these records are on a separate line (the record is a number with no delimiter).
Some lines contain the value 0 and other lines contain the value 1.
How do I create a print statement that assigns a line number to all the lines that contain the value 1?

For example: Let’s say that the 1st ten lines of the text file look like this:

0
0
1
0
1
0
0
1
1
0

How do I create multiple print statements that say:

Line #3 contains the value of 1
Line #5 contains the value of 1
Line #8 contains the value of 1
Line #9 contains the value of 1

I only want to display the lines that contain the value of 1 and have a way to display which line # contains that value.

I hope this makes sense,

Jason

All you need is to read your text file in array, and then print # within a loop:
[php]<?php
$rows = file(‘mydir/data.txt’);
if(count($rows)) foreach($rows as $n=>$row){
if(substr($row,0,1)==‘1’) echo ‘Line #’.($n+1).’ contains the value of 1.
’;
}
?>[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service