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
PHP
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
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
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
"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.
ASKER
Thanx for your help and info
ASKER
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