GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 21-Oct-2005, 08:50
phrygianphreak phrygianphreak is offline
New Member
 
Join Date: Oct 2005
Posts: 9
phrygianphreak is on a distinguished road

Multidimensional Array


I'm trying to read in a file of data that has multiple lines that contain a date, timestamp, and other data. What I'm wanting to do is use the timestamp, and only print out every 4th second worth of data by line. I was thinking of using a multidimensional array, but not sure how. Here is the first few lines of the file:

Code:
14-Oct-05 13:44:15 35.40335234 -97.60877773 6057.912 14-Oct-05 13:44:16 35.40338272 -97.60940025 6057.555 14-Oct-05 13:44:17 35.40341299 -97.6100225 6057.387 14-Oct-05 13:44:19 35.40344255 -97.61064434 6057.097 14-Oct-05 13:44:20 35.40347207 -97.61126581 6056.618 14-Oct-05 13:44:21 35.40350261 -97.61188696 6055.828 14-Oct-05 13:44:22 35.40353402 -97.61250795 6054.819 14-Oct-05 13:44:23 35.40356621 -97.61312883 6053.694

The array I've created is

PHP Code:

<?php

// FILENAME: dataarray.PHP

   $linenumber['number']=array(
                  'dt'=>[str date],
                  'ts'=>[int timestamp],
                  'lat'=>[int latitude], 
         'long' =>[int longitude],
         'alt' =>[int altitude]);

?>


I'm pretty familiar with the file i/o, but just need help with the script to sort the lines by the timestamp.
Thank you.
Last edited by phrygianphreak : 21-Oct-2005 at 09:11. Reason: Please insert your Php code between [php] & [/php] tags
  #2  
Old 21-Oct-2005, 12:29
cable_guy_67's Avatar
cable_guy_67 cable_guy_67 is offline
Senior Member
 
Join Date: Oct 2004
Location: Nescopeck, PA
Posts: 1,109
cable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the roughcable_guy_67 is a jewel in the rough

Re: Multidimensional Array


Quote:
Originally Posted by phrygianphreak
I'm trying to read in a file of data that has multiple lines that contain a date, timestamp, and other data. What I'm wanting to do is use the timestamp, and only print out every 4th second worth of data by line.

Hello and Welcome to GIDForums™ phrygianphreak. By every fourth second do you mean if you get the first timestamp seconds, (in your example 15) you would like to print out that line and then not print out another until 4 seconds have passed (in this case 19 or higher)?

Mark
__________________
"Opportunity is missed by most people because it comes dressed in overalls and looks like work."
--Thomas Alva Edison
"Those who would give up essential liberty to purchase a little temporary safety, deserve neither liberty nor safety."
--Benjamin Franklin
"A happy person is not a person in a certain set of circumstances, but rather a person with a certain set of attitudes."
--Hugh Downs
  #3  
Old 21-Oct-2005, 14:14
phrygianphreak phrygianphreak is offline
New Member
 
Join Date: Oct 2005
Posts: 9
phrygianphreak is on a distinguished road

Re: Multidimensional Array


What I mean is that I want to output the first line, with the 13:44:15 timestamp, then skip 4 seconds and print out the line with 13:44:20, and so on in that fashion until all the lines have been printed out. Does that make sense?
  #4  
Old 22-Oct-2005, 03:03
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Multidimensional Array


Hello phrygianphreak,

I assume you've already seen this page: http://www.desilva.biz/arrays/filter.html?

Because you left out a lot of details, I will assume a lot of things about the data file. If you look at your example data file, you will notice that the record for 13:44:18 is missing.

I'll assume that data is recorded every second and that there is no duplicate timestamps and no missing seconds. Also I'll asume new records are appended to the data file. In other words, the last record is right at the bottom of the file, as it is in your example file.

Having assumed all that, here's how my dataarray.PHP will look like (please read comments to understand what it's supposed to do):

PHP Code:

<?php
// FILENAME: /DATAARRAY.PHP

// where is the data file?
$data_file = './example.dat'; // relative path.

// you can set the interval in seconds.  Default is 5 seconds.
// e.g. http://www.example.com/dataarray.php?interval=2
$interval = 5;
if( isset($_REQUEST['interval']) )
{
  $interval = intval( $_REQUEST['interval'] );
  if( $interval<1 )
    $interval = 5; // invalid value, so use default.
}

// open tag for the table of records.
echo '<table style="border-collapse:collapse;" cellspacing="0" cellpadding="3" border="1">' .
     '<tr><th>date</th><th>time</th><th>latitude</th><th>longitude</th><th>altitude</th></tr>';

// open the file for reading.
if( !$fp=@fopen($data_file, 'rb') )
  trigger_error( "Cannot open file <var>$data_file</var> for reading." );
// while not EOF, grab a line, process the data and print the matching line.
while( !feof($fp) )
{
  $line = trim( @fgets($fp, 4096) );
  if( $line )
  { // process the line of data.
    $tmp = explode( "\t", $line ); // $tmp[1] is the `time` data.
    if( !isset($ts) )
    { // do this once and only with the FIRST line.
      $time_str = $tmp[1];
      $ts       = strtotime( $tmp[1] );
    }
    if( $tmp[1] == $time_str )
    { // matching line.
      // alternate the row (background) colours.
      $bc = ( $bc==' style="background-color:#dadada;"' ? null : ' style="background-color:#dadada;"' );
      echo "<tr><td$bc>$tmp[0]</td><td$bc>$tmp[1]</td><td$bc>$tmp[2]</td><td$bc>$tmp[3]</td><td$bc>$tmp[4]</td></tr>";
      // set the next timestamp to look for.
      $ts       = strtotime( "+$interval seconds", $ts );  // adds [int interval] seconds to the previous unixtimestamp (UTS).
      $time_str = date( "H:i:s", $ts );  // formats the UTS into something like "13:44:20"
    }
  }    
}
// close the data file.
@fclose( $fp );

// if $data_file is empty, we say so...
if( !isset($time_str) ) // since this variable is set only if there was at least ONE row of data in the file.
  echo "<tr><td colspan=\"5\">No records found.</td></tr>";
// close the table of records.
echo "</table>";
?>


I just tried this script (with your example data file) and it fails only when you use INTERVAL=3, since the record for the 18th second is missing!
  #5  
Old 22-Oct-2005, 07:28
phrygianphreak phrygianphreak is offline
New Member
 
Join Date: Oct 2005
Posts: 9
phrygianphreak is on a distinguished road

Re: Multidimensional Array


That's perfect,JdS. Your assumptions must have been pretty intuitive because it did exactly what I wanted it to!
Thank you!
  #6  
Old 22-Oct-2005, 18:27
phrygianphreak phrygianphreak is offline
New Member
 
Join Date: Oct 2005
Posts: 9
phrygianphreak is on a distinguished road

Re: Multidimensional Array


I was going over the data after it got processed, and for some reason it stops halfway through the file. The file goes up to about 14:00:55, but for some reason it stops at 13:50:10. I tried increasing the numbrer in the EOF from 4096 to a higher number, but that doesn't seem to have any affect on the file.
Here's the file if you want to try it.
Attached Files
File Type: txt data.txt (52.4 KB, 3 views)
  #7  
Old 24-Oct-2005, 12:12
phrygianphreak phrygianphreak is offline
New Member
 
Join Date: Oct 2005
Posts: 9
phrygianphreak is on a distinguished road

Re: Multidimensional Array


Hey, JdS, did you get my last post? I need to see if I can get the code to read the whole file.
Thanks!
  #8  
Old 25-Oct-2005, 06:26
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Multidimensional Array


I already mentioned that the script will fail if there are missing seconds.

If you analysed and understood how the example script works, you would have figured this out yourself too.
  #9  
Old 25-Oct-2005, 07:23
phrygianphreak phrygianphreak is offline
New Member
 
Join Date: Oct 2005
Posts: 9
phrygianphreak is on a distinguished road

Re: Multidimensional Array


Unfortunately, it does sometimes skip a second, and there are duplicate seconds. Is there no way around this?
  #10  
Old 25-Oct-2005, 07:37
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Multidimensional Array


Of course there is a way around; there is always a way around.

Like I said, you didn't supply a lot of details with your original question, so I had to assume a lot of essential details.

To fix this example to work for you, you just simply need to convert the formatted time string i.e. 13:44:23 into a unixtimestamp for each row.

You compare that value to the next interval's unixtimestamp. If it fails to match, you must test to see if this row's unixtimestamp is later than the interval's timestamp. If it's true, you set the next, new interval timestamp and proceed.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26
Linear search on a multidimensional array. anignna C++ Forum 4 07-Mar-2004 20:07
Array_search on a multidimensional array JdS MySQL / PHP Forum 3 11-May-2003 06:22

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 06:08.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.