0

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 known 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?
2
  • 3
    You should use = instead of .=, you're not concatenating a string. Commented Oct 26, 2010 at 3:53
  • +1 Thanks. The code mutated along the way and that was overlooked. Commented Oct 26, 2010 at 4:51

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess I didn't explain clearly. I'll updated the question. +1 for replying. Thanks.
1

Can you just store a reference to the widget which the timestamp is associated with along with the timestamp?

1 Comment

+1 Thanks. It looks like the question was not clear, so I have updated it.
1

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.

1 Comment

+1 Sorry that it's not clear. I have updated the question & hope that that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.