Jump to content

Loop through mulit-dimension array and match dates


meltingpoint

Recommended Posts

example structure of text database-  mytextfile.txt

10-11201|2010/09/01|Sam|Thurston

10-11307|2010/09/04|Tony|Piper

10-11405|2010/09/11|Sarah|Smith

 

<?php
$file2 = 'mytextfile.txt';

$openedfile	=fopen($file2, "r") or die("ERROR- could not open file for editing.");
//
flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit.");
$hold[$record_count] = explode("|", trim(fgets($openedfile)));
while(!feof($openedfile))
{
$record_count++;
$hold[$record_count] = explode("|", trim(fgets($openedfile)));
}
?>

 

What I need to do is loop through this and one by one- take any arrays that contain a date that is between $dateX and $dateY (which comes via a form input) and place it in a new array $matched.  I am stumped. 

1-Is there a way to do it inside the above while loop that I am not seeing?

2- Do I need to open the file in another manner? 

2- Would it be best to now loop through $hold[$record_count]

 

I am trying to keep the process short so as not to use up too much memory.  Point me in the right direction for this one please.

 

:shrug:

1-Is there a way to do it inside the above while

 

^^^ Yes.

 

If the only processing you will do on the data is to retrieve matching records, so that you don't actually need to store all the records in memory, then yes, you should add a conditional test inside of the while(){} loop so that you only keep the matching records.

 

If all your date values in the file are in the same exact format yyyy/mm/dd, with leading zeros in the mm and dd values and the $dateX and $dateY are both also in that same exact format, then you can compare the date values directly using greater-than/less-than comparisons and form an if(){} statement that only saves records that have a date field between the $dateX and $dateY values.

Wonderful.  Yes- the database dates and form dates are formated the same.  Yes- I need to do additional things with the remaining data- that is why I want to save those records in an array $matched so that I can do other things with them and then output the results.

 

Problem is that the first iteration:

$hold[$record_count] = explode("|", trim(fgets($openedfile)));

takes the $openedfile and puts the first line into $hold[1] and then continues on until the end of the file with the

loop.  So the first line is not counted in the conditional statement.  Also- if there is no record that INITIALLY

meets the conditional statement inside the while loop- it does not reach the $record_count++.

 

Here is what I have tried.

$file = 'mytextfile.txt';

$openedfile	=fopen($file2, "r") or die("ERROR- could not open file for editing.");
//
flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit.  Please try again");
$hold[$record_count] = explode("|", trim(fgets($openedfile)));
while(!feof($openedfile))
{
if($hold[$record_count][1] >= $dateX && $hold[$record_count][1] <= $dateY)
{
$record_count++;
$hold[$record_count] = explode("|", trim(fgets($openedfile)));
}

 

There is much wrong with this- but I am stumped how to: A- discard data not needed and B- Create new $matched array of data that meets the requirement.

 

<?php
$file2 = 'mytextfile.txt';
$openedfile = fopen($file2, "r") or die("ERROR- could not open file for editing.");
flock($openedfile, LOCK_EX) or die("Error!- Could not obtain exclusive lock on the file to edit.");

$dateX = '2010/09/04'; // simulate test data
$dateY = '2010/09/04';

while(!feof($openedfile)){
$record = explode("|", trim(fgets($openedfile)));
if($record[1] >= $dateX && $record[1] <= $dateY){
	$matched[] = $record;
}
}

echo '<pre>',print_r($matched,true),'</pre>';
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.