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

LVL 1
trevor1940Asked:
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.

gr8gonzoConsultantCommented:
If:
#1. It's a small file
#2. It's always pipe-delimited, with no quoted values
#3. The order is identical to what you have (ID, then Type)

...then the fastest way would be to do this:
$matched = (strpos(file_get_contents("log.txt"),"123|ABC") !== false); // $matched will be TRUE if it finds that combination of ID and Type

// Check the results
if($matched)
{
  echo "Found that combination of ID and Type!";
}
else
{
  echo "Didn't find that combination of ID and Type!";
}

Open in new window


If it's a big file, but the rest of the criteria is true (pipe-delimited, no quoted values, consistent order of columns), you can use fopen/fread:
$fp = fopen("log.txt","r");
while(!feof($fp))
{
  $line = fgets($fp);
  $matched = (strpos($line,"123|ABC") !== false);
  if($matched) { break; } // Don't bother searching the rest of the file if we already have a match
}
fclose($fp);

// Check the results
if($matched)
{
  echo "Found that combination of ID and Type!";
}
else
{
  echo "Didn't find that combination of ID and Type!";
}

Open in new window


If there is a chance of having quoted values, then you should use fgetcsv:
$fp = fopen("log.txt","r");
while(!feof($fp))
{
  $line = fgetcsv($fp,0,"|");
  $matched = (($line[0] == 123) && ($line[1] == "ABC"));
  if($matched) { break; } // Don't bother searching the rest of the file if we already have a match
}
fclose($fp);

// Check the results
if($matched)
{
  echo "Found that combination of ID and Type!";
}
else
{
  echo "Didn't find that combination of ID and Type!";
}

Open in new window

1

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
Ray PaseurCommented:
Maybe consider setting up the log file as a JSON string, so you can turn it into an object and use SimpleXML.

Please see https://iconoun.com/demo/temp_trevor1940.php
<?php // demo/temp_trevor1940.php
/**
 * https://www.experts-exchange.com/questions/29007662/Test-if-unique-ID-is-in-log-file.html
 */
error_reporting(E_ALL);

// A LOG FILE, AS IF READ BY file_get_contents()
$log = <<<EOD
ID|TYPE|DATE
123|XYZ|2017-03-07
123|ABC|2017-03-08
124|XYZ|2017-03-08
EOD;

// MAKE AN ARRAY OF ROWS
$arr = explode(PHP_EOL, $log);

// GET THE KEYS
$top = $arr[0];
unset($arr[0]);
$top = explode('|', $top);

// ASSOCIATE KEYS WITH VALUES
foreach ($arr as $line)
{
    $line = explode('|', $line);
    $data = array_combine($top, $line);

    // TEST
    if ($data['TYPE'] == 'ABC') print_r($data);
}

Open in new window

Outputs:
Array ( [ID] => 123 [TYPE] => ABC [DATE] => 2017-03-08 )

Open in new window

0
trevor1940Author Commented:
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
0
Ray PaseurCommented:
"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.
1
trevor1940Author Commented:
Thanx for your help and info
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.