我有以下数组
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 ......等等...这样可以得到最终结果:感谢您抽出宝贵时间。
答案 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);
答案 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;
}
}