Load .txt into PHP array and only output certain rows

I have a text file that I need to open via PHP and load into an array, this much seems to work so far.  What I am having problems with is I only want to display rows where Employee ID equals a specified value.

This is something that I cannot get via Mysql and can only get out of a .txt file.  How can I get it to only echo the lines where $value= 141991 (for example)?

Employee ID      Punch Time
141991      5:43:00
142353      5:37:00
145106      6:52:00
149205      5:39:00
149205      9:41:00
149229      6:39:00
149244      9:00:00
149319      6:43:00
<?php

$myfile = '../Attendance/current-punches.txt';
$lines = file($myfile);   

$value= "204445";

for($i=count($lines);$i>0;$i--)

{
if ($lines[i] = $value)
{
echo $lines[$i];}
}

?>

Open in new window

raynman1972Asked:
Who is Participating?
 
hieloCommented:
Refer to:
http://us3.php.net/manual/en/function.fgetcsv.php

assuming each column is separated by a tab:
<?php
$value= "204445";
$file="../Attendance/current-punches.txt";
if (($handle = fopen($file, "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
		if($data[0]==$value)
		{
			echo implode("\t",$data);
		}
        }
    }
    fclose($handle);
}
?>

Open in new window

0
 
karoldvlCommented:
Assuming your fields are tab separated:
<?php

$myfile = '../Attendance/current-punches.txt';
$lines = file($myfile);   

$value = "149205";

foreach($lines as $line) {
	$fields = explode("\t", $line);
	
	if ($fields[0] == $value) {
		echo $fields[0]."\t".$fields[1]."\n";
	}
}

?>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.