简体   繁体   English

PHP根据时间拆分数组值

[英]PHP Split array values based on time

I want this human readable words into php code: 我希望将这些人类易读的单词转换为php代码:

Take all values from column "Ratings" WHERE Time >= 24 hours Split them into 1 hour each Take average Rating values for each hour and put them into 24 variables 从“评级”列中获取所有值,其中“时间”> = 24小时将其分成1小时,每个小时取平均评级值,并将其分为24个变量

WhatI'm doing now is a bad long code :/ 我现在正在做的是很长的代码:/

I'm taking them an hour after another, each one in a separate function where I specify an hour via mysql select statement! 我花了一个小时接一个小时,每个人都在一个单独的函数中,我通过mysql select语句指定一个小时!

I'm new in programming, and I couldn't figure out what to do to get all values at once, and split them into 1 hour each 我是编程的新手,我不知道该怎么做才能一次获取所有值,并将它们分别分成1小时

Thanks 谢谢

EDIT : changed the fiddle, so it uses time as an int, that represents seconds since January 1st, 1970. 编辑 :更改了小提琴,所以它使用时间作为整数,表示自1970年1月1日以来的秒数。

Take a look at this fiddle: http://sqlfiddle.com/#!2/6e6b6/5/0 It is returning the rating average on a hourly basis. 看一下这个小提琴: http : //sqlfiddle.com/#!2 /6e6b6/5/0它每小时返回一次平均评分。 Is this that you need? 这是您需要的吗? I think you might tune the query for your other needs. 我认为您可能会针对其他需求调整查询。

You can read more about it at http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html 您可以在http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html上阅读有关它的更多信息。

Got it, after so many searches. 经过如此多的搜索后明白了。 Learning is beautiful :) 学习是美好的:)

This is the code 这是代码

$mysql = "select date_format(from_unixtime(Time), '%H') as hour, avg(Rate) from za7ma1 group by hour";

$result = mysql_query($mysql) or die(mysql_error());

            // Print out result
    while($row = mysql_fetch_array($result)){
        echo "The average for this hour ". $row['hour']. " is ".$row['avg(Rate)'];
        echo "<br />";
    }

Thanks to Alexander Jardim for the great tool http://sqlfiddle.com/#!2/6e6b6/5/0 which opened my eyes on avg() mysql function 感谢Alexander Jardim的出色工具http://sqlfiddle.com/#!2/6e6b6/5/0 ,它使我对avg()mysql函数大开眼界

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM