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:

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

<?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>';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.