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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.