我有一个由MySQL生成的数组,其中包含本周的访问者:
Array
(
[0] => Array
(
[iid] => 2
[mid] => 123456
[name] => Username
[date] => 2013-09-03 18:19:23
)
[1] => Array
(
[iid] => 2
[mid] => 123456
[name] => Username
[date] => 2013-09-03 18:19:20
)
[2] => Array
(
[iid] => 2
[mid] => 123456
[name] => Username
[date] => 2013-09-03 18:10:42
)
)
每个密钥都是访问者,我需要每天计算阵列中有多少访问者。
此数组应返回:
Mon: 0
Tue: 3
Wed: 0
etc
答案 0 :(得分:1)
通过谷歌搜索和查找Count by grouping Multidimensional arrays in PHP我已经修改了代码,这似乎解决了你的问题:
$inputArray = array(
array('iid' => 5, 'mid' => '123456', 'name' => 'Username', 'dd' => '2013-09-03 18:19:23'),
array('iid' => 5, 'mid' => '123456', 'name' => 'Username', 'dd' => '2013-09-03 18:19:20'),
array('iid' => 5, 'mid' => '123456', 'name' => 'Username', 'dd' => '2013-09-03 18:10:42'),
array('iid' => 5, 'mid' => '123456', 'name' => 'Username', 'dd' => '2013-09-04 18:19:23'),
array('iid' => 5, 'mid' => '123456', 'name' => 'Username', 'dd' => '2013-09-04 18:19:23'));
$outputArray = array();
foreach ( $inputArray as $record ) {
// get only the first part of the date string i.e. 2013-09-03
$day = explode(' ', $record['dd']);
// if the date has not already been added to the outputArray, add it
if ( !key_exists($day[0], $outputArray) ) {
$outputArray[$day[0]] = 0;
}
$outputArray[$day[0]] += 1;
}
echo '<pre>';
print_r($outputArray);
<强>输出:强>
Array
(
[2013-09-03] => 3
[2013-09-04] => 2
)
您可以通过复制和粘贴http://writecodeonline.com/php/
来测试它