php - Count visitors of each day -
i've got array being generated mysql contains visitors of current week:
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 ) )
each key visitor, need count each day how many visitors there in array.
this array should return:
mon: 0 tue: 3 wed: 0 etc
as learned googling , finding count grouping multidimensional arrays in php have modified code , seems solve problem:
$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 $record ) { // first part of date string i.e. 2013-09-03 $day = explode(' ', $record['dd']); // if date has not been added outputarray, add if ( !key_exists($day[0], $outputarray) ) { $outputarray[$day[0]] = 0; } $outputarray[$day[0]] += 1; } echo '<pre>'; print_r($outputarray);
output:
array ( [2013-09-03] => 3 [2013-09-04] => 2 )
you can test copying , pasting here http://writecodeonline.com/php/
Comments
Post a Comment