从日期数组中按天显示出现次数

时间:2012-08-12 21:33:47

标签: php date multidimensional-array

我正在开发一个统计应用程序,我希望输出白天发生的交互量。

我有一个多维数组,可以从数据库中提取所有信息,这是一个例子:

 [0] => Array
            (
                [date] => 2012-07-26
                [location] => 709c6d241674ca22
                [action] => start_scan
            )

        [1] => Array
            (
                [date] => 2012-07-26
                [location] => 709c6d241674ca22
                [action] => scan_displayed
            )

        [2] => Array
            (
                [date] => 2012-07-27
                [location] => 709c6d241674ca22
                [action] => lower_device
            )

        [3] => Array
            (
                [date] => 2012-07-27
                [location] => 709c6d241674ca22
                [action] => how_to_use_displayed
            )

        [4] => Array
            (
                [date] => 2012-07-27
                [location] => 709c6d241674ca22
                [action] => raise_device
            )

        [5] => Array
            (
                [date] => 2012-07-28
                [location] => 709c6d241674ca22
                [action] => scan_displayed
            )

我可以通过格式化日期来确定每次互动发生的日期:

date('D', strtotime('2012-07-26'));

我的问题是如何计算一周中每天发生的交互次数,然后将其输出,例如:

[Sunday] => 2
[Monday] => 3
[Tueday] => 1
[Wednesday] => 5
[Thursday] => 10
[Friday] => 4
[Saturday] => 9

非常感谢任何建议!

1 个答案:

答案 0 :(得分:1)

$dates = array();
foreach($data as $item) {
    $day = date('l', $item['date']);
    $dates[$day]++;
}
var_dump($dates);

您可能希望将数组声明为$ dates = array('Monday'=> 0,'Tuesday'=> 0 ....)以获取包含没有交互的日期的数组。