views:

116

answers:

5

I have to put datas every 10 seconds in an array. Is it silly to index this array with modified timestamps

$a[timestamp] = 54; $a[timestamp+10] = 34;

or in Javascript with the setInterval() and passing via Ajax the index (very crappy to me) ?

or have I a best option ?

Further details :

  • I have to link the real-time with entries in my array : this is my problem. At the 3rd cycle (21 sec to 30 sec from the beginning time).
  • I have only 15 entries to store.
  • My present code :

    $first_time = (int)date('Hi'); $_SESSION['mypile'][$first_time] = array_fill ($first_time, 15, array('paramA' => 0, 'paramB' => 0));

then, the Ajax part calls this script :

$time = (int)date('Hi');
$_SESSION['mypile'][$time]['paramA'] = calcul_temp($_SESSION['mypile'], $time);
+2  A: 

Why would you not use a plain numerically indexed array? If you don't need the timestamp, then:

$a[] = 54; 

$a[] = 34;

If you do need the timestamp, then it would make more sense to do something like:

$a[] = array('timestamp' => time(), 'number' => 54);

$a[] = array('timestamp' => time(), 'number' => 34);

Then at each offset you have a more meaningful associative array:

echo 'Timestamp: ' . $a[0]['timestamp'] . ', Number: ' . $a[0]['number'];

If those operations happen in rapid succession, you would probably be better using microtime

karim79
Why does that make more sense? Why not just index with the time?
Brian Ramsay
Like I added, I need to link the present timestamp to my array.Maybe I'll do this in Javascript...
mere-teresa
That does work, but traversing is more fun if it's a plain array.
karim79
A: 

That seems like a very good solution, though you will have to be careful about memory usage if the script will be running for a long time.

Brian Ramsay
ok, I should have been more specific, I have only 15 values to keep.
mere-teresa
A: 

That is fairly silly; if you've set a time interval, simply have your function be called every 10 seconds and add your new number to the next index in the array. Keep track of this index globally or within the scope of the iteration.

AlbertoPL
What if he needs to record the timestamp? If so, this is a good method.
BabyCakes
It's every 10 seconds, 15 times, then stop.I have also this idea, like karim79 said: let PHP index the array, and call my PHP-ajax every 10 seconds.
mere-teresa
Yes, that is exactly what you should do.
AlbertoPL
A: 
$a['timestamp'] = time();
while (true) { 
  $a['data'][] = getData(); 
  sleep(10); 
}

You could make a class of it. The construct then sets the timestamp, and with SPL array index and iterator it can be looped in foreach and used with some array functions. You can make a method to get an array with or without the timestamp, etc.

$dataCycle = new DataCycle();
while(true) {
  $dataCycle->addData(getData());
  sleep(10);
}
OIS
I want to keep my system as simple as possible, it's a POC.
mere-teresa
A: 

Ok so I decided to round my timestamp every 10 seconds to have pieces of time. Simple, silly and work for me.

Thank you for ideas.

mere-teresa