我需要为该日期创建一个具有:日期和事件数量的关联数组。
想象一下数据库中的这个表(XPTO):
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现在,我想要的结果是: 09.10.2013 12:43:54,2,23.07.2013 10:32:31,3.有什么可以帮助我的吗?
发布于 2013-09-15 18:19:50
SQL可以做到这一点。
$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];如果您想要完整的信息而不是计数:
$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;
}现在有了这个数组,结果是:
[2013-07-23 10:32:31] => array (
array (
'id'=>2,
'parameter'=>54,
'sensor_id'=>65,
),
array (
'id'=>3,
'parameter'=>23,
'sensor_id'=>45,
),
...请根据数据库连接类型(PDO等)修改以下代码
https://stackoverflow.com/questions/18815807
复制相似问题