计算日期相同的数组中的数据并构建图表栏morrisjs

时间:2014-07-30 20:57:25

标签: php arrays

我有以下数组

array (    [0] => 2014-06-01 12:45:02
    [1] => 2014-06-01 12:45:03
    [2] => 2014-06-01 12:48:52
    [3] => 2014-06-01 12:50:31
    [4] => 2014-06-01 13:18:49
    [10] => 2014-06-01 13:33:16
    [11] => 2014-06-01 14:20:48
    [12] => 2014-06-01 14:24:44
    [13] => 2014-06-01 15:28:16
    [14] => 2014-06-02 10:50:56
    [15] => 2014-06-02 10:51:07
    [16] => 2014-06-02 18:16:13
    [17] => 2014-06-03 11:20:20
    [18] => 2014-06-03 11:58:46
    [19] => 2014-06-03 11:59:03
    [20] => 2014-06-03 12:00:51
    [21] => 2014-06-03 12:01:21
    [22] => 2014-06-04 19:03:17
    [23] => 2014-06-04 19:06:03
    [24] => 2014-06-04 19:12:03
    [25] => 2014-06-04 21:18:03
)
等等......大约30条记录,想法是得到多少“日期”“相同”2014-06-01,2014-06-02,2014-06-03 ......等等...这样可以得到最终结果:
从2014-06-01之日起,您已记录(9)次
从2014-06-02开始,您记录了(3)次
......等 有了这些信息,我可以建立一个morrisjs图表栏,但我不知道如何计算日期......现在我可以将这些数据与“最后”月份或年份进行比较......但就像我说的那样,我有不知道如何计算记录,因为当我使用array_count_values时它也检查时间,我不需要时间只是日期,结果不正确,因为它统计所有记录而不是“组”记录从同一天开始......

感谢您抽出宝贵时间。

5 个答案:

答案 0 :(得分:2)

array_count_values()将返回一个数组,其中日期为键,以及它们作为值出现在数组中的次数。但首先,建立一个没有时间的日期数组:

foreach($array as $value) {
    $dates[] = date('Y-m-d', strtotime($value));
}
$counts = array_count_values($dates);

答案 1 :(得分:2)

尝试这样的事情:

$data = array (    '0' => '2014-06-01 12:45:02',
    '1' => '2014-06-01 12:45:03',
    '2' => '2014-06-01 12:48:52',
    '3' => '2014-06-01 12:50:31',
    '4' => '2014-06-01 13:18:49',
    '10' => '2014-06-01 13:33:16',
    '11' => '2014-06-01 14:20:48',
    '12' => '2014-06-01 14:24:44',
    '13' => '2014-06-01 15:28:16',
    '14' => '2014-06-02 10:50:56',
    '15' => '2014-06-02 10:51:07',
    '16' => '2014-06-02 18:16:13',
    '17' => '2014-06-03 11:20:20',
    '18' => '2014-06-03 11:58:46',
    '19' => '2014-06-03 11:59:03',
    '20' => '2014-06-03 12:00:51',
    '21' => '2014-06-03 12:01:21',
    '22' => '2014-06-04 19:03:17',
    '23' => '2014-06-04 19:06:03',
    '24' => '2014-06-04 19:12:03',
    '25' => '2014-06-04 21:18:03',
);
foreach ($data as $key => $value) {
    $array= explode(" ",$data[$key]);
   $array_result[] = $array[0];
}
$vals = array_count_values($array_result);

print_r($vals);

<强>输出

Array
(
    [2014-06-01] => 9
    [2014-06-02] => 3
    [2014-06-03] => 5
    [2014-06-04] => 4
)

答案 2 :(得分:1)

首先,使用array_map()从日期中删除时间。然后使用array_count_values()计算日期。

$new_array = 
    array_map(function($x){return date('Y-m-d', strtotime($x));}, $array);

$date_count = array_count_values($new_array);

See demo

答案 3 :(得分:1)

基于user3350731解决方案,但使用substr()而不是explode()。

<?php

$data = array (
    '0' => '2014-06-01 12:45:02',
    '1' => '2014-06-01 12:45:03',
    '2' => '2014-06-01 12:48:52',
    '3' => '2014-06-01 12:50:31',
    '4' => '2014-06-01 13:18:49',
    '10' => '2014-06-01 13:33:16',
    '11' => '2014-06-01 14:20:48',
    '12' => '2014-06-01 14:24:44',
    '13' => '2014-06-01 15:28:16',
    '14' => '2014-06-02 10:50:56',
    '15' => '2014-06-02 10:51:07',
    '16' => '2014-06-02 18:16:13',
    '17' => '2014-06-03 11:20:20',
    '18' => '2014-06-03 11:58:46',
    '19' => '2014-06-03 11:59:03',
    '20' => '2014-06-03 12:00:51',
    '21' => '2014-06-03 12:01:21',
    '22' => '2014-06-04 19:03:17',
    '23' => '2014-06-04 19:06:03',
    '24' => '2014-06-04 19:12:03',
    '25' => '2014-06-04 21:18:03',
);

foreach ($data as $key => $value) {
   $array_result[] = substr($value, 0, 10);
}

$vals = array_count_values($array_result);

print_r($vals);

注意:不要调用strtotime(),date()或array_map()。

答案 4 :(得分:0)

制作另一个包含日期和计数器的变量。在您的初始数组中查看日期,并将1添加到另一个数组中的计数器。

示例:

$dateCounters = array();
foreach($initialArray as $date){
    $niceDate = date('Y-m-d', $date);
    if(in_array($dateCounters, $niceDate){
        $dateCounters[$niceDate] += 1;
    } else {
        $dateCounters[$niceDate] = 1;
    }
}