Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

Test if unique ID is in log file

I have a log file something like bellow

How do I test if an ID exist and type = ABC?

log.txt

ID|TYPE|DATE
123|XYZ|2017-03-07
123|ABC|2017-03-08
124|XYZ|2017-03-08

Open in new window


PHP

<?php
error_reporting(E_ALL);
$Data = explode('|', file_get_contents("log.txt"));
trace($Data);
$ID = 123;
if($Data[$ID]){
  echo "found $ID";
  }

function trace($arr){
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}//end trace

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

I hadn't realized the size of the file mattered

The log file currently doesn't exist so can be in what  ever format I want but is likely to grow daily as each time the application is run new lines will be created maybe 10 per day.

Maybe I need further explanation

each time it's run a directory is read with 1 or more files in
within each file a ID and TYPE is read
I need to test it hasn't been processed  before potentially the same ID can be used up to 3 times depending on type  however the vast majority will be once

so I would open the log file once and as I read the directory test if that file ID / TYPE has been processed before if not then process the file
"Size matters" within some broad constraints.  When we're talking about adding 10 lines a day to a file, it will take years before size matters!  Any of the solutions shown here will probably be OK, but as usual, the devil is in the details.  If the test case we worked with is not truly representative of the live data, then there may be additional considerations and changes needed.
Thanx for your help and info