I need an efficient PHP array lookup
Let's say that I have some widgets, and each widget has events. Each event has a unique timestamp and a non-unique value.
Since the timestamp is rather large to use as an index (hashing?) I am adding events like this:
$_SESSION['widgets']['datasets'][$i][] =
array('timestamp' => $ts, 'value' => $value);
However, not every widget has an event at every timestamp, so there are gaps. EDIT: that means that maybe widget A is entered Mon, Tue, Wed and field B is entered Mon, Wed, Fri. I will then loop through Mon, Tue, Wed, Thur, Fri, Sat, Sun, and want to know which widget had an event that day and what its value was.
After reading all the events from the database, I loop over all k开发者_如何学运维nown timestamps, then loop over each widget to see if it has an event with that timestamp.
And there I am probably inefficient.
What's the best way to code this?
for each timestamp
for each widget
if the widget has an entry with the timestamp, get the value <=== how?
Since you're grabbing this data from the database, would it be possible to change your query to only return records with a timestamp? That way there would be no need to do anything to the data once you get it back.
Can you just store a reference to the widget which the timestamp is associated with along with the timestamp?
Despite your edits I still don't think it's clear what it is you're trying to do.
Is this an exercise in retrieving some data in sorted order? Are you wanting help with your database SQL code or with PHP code to process it afterwards? What are these widgets and events we're talking about, and what are we doing with them?
Since the timestamp is rather large to use as an index (hashing?)
I don't see why a timestamp would be too large to use as an index. And are you talking about an 'index' in the database or a 'key' as used in a PHP array? Either way, I don't see how it would be too large, if what you call a timestamp is what I imagine a timestamp to be.
Each event has a unique timestamp and a non-unique value.
This seems unusual - did you mean it the other way around? Perhaps if you could say what the events are I could understand better.
精彩评论