1

I need to create an associative array with: date and number of events for that date.

Imagine this table (XPTO) in the database:

timestamp id parameter sensor_id 2013-09-10 12:43:54 1 34 3 2013-07-23 10:32:31 2 54 65 2013-07-23 10:32:31 3 23 45 2013-07-23 10:32:31 4 12 1 2013-09-10 12:43:54 5 1 43 

and now, the result that I want is the following: [09.10.2013 12:43:54, 2], [23.07.2013 10:32:31,3]. Any idea to help me?

1 Answer 1

1

SQL does the trick.

$myarray = array(); $result = mysql_query("SELECT timestamp, count(id) FROM xpto GROUP BY timestamp"); while ($row = mysql_fetch_array($result)) $myarray[$row[0]]=$row[1]; 

If you want full information instead of counting:

$myarray = array(); $result = mysql_query("SELECT * FROM xpto ORDER BY timestamp"); while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $timestamp=$row['timestamp']; unset($row['timestamp']); $myarray[$timestamp][]=$row; } 

Now you have this array as a result:

[2013-07-23 10:32:31] => array ( array ( 'id'=>2, 'parameter'=>54, 'sensor_id'=>65, ), array ( 'id'=>3, 'parameter'=>23, 'sensor_id'=>45, ), ... 

Please modify following code based on your database connection type (PDO, etc)

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

2 Comments

But how do I create an associative array with the events that occur for a particular date?
I think I already answered this question by editing at the same time.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.