我试图让我的函数以正确的顺序显示月份,它在序列中的错误位置获得六月、七月和五月。
我非常感谢一些帮助,以使其正常工作。
public function get_incident_data()
{
$all_incidents = $this->incidents_m->get_all_by_date();
// Loop through and find & replace month number with name
$months_name = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$months_date = array('01','02','03','04','05','06','07','08','09','10','11','12');
/**
* Take the array and explode it to get
* the month number and replace it with the months name.
*
* Output: [0] => Jan [1] => Feb [2] => Feb [3] => Feb [4] => Mar [5] => Apr ...
*/
$months_are = array();
foreach($all_incidents as $key => $value)
{
$date_bits = explode('-', $value['incident_date']);
if($date_bits[1] != 00)
{
$months_are[] = str_replace($months_date, $months_name, $date_bits[1]);
}
}
/**
* Group and count all monthly incidents.
*/
$result = array();
foreach($months_are as $k => $v){
if(isset($result[$v])){
$result[$v]++;
}
else {
$result[$v] =1;
}
}
/**
* Currently Outputs: Array ( [Jan] => 1 [Feb] => 29 [Mar] => 66 [Apr] => 64 [Aug] => 1 [Sep] => 4 [Oct] => 2 [Nov] => 2 [Dec] => 1 [Jun] => 1 [Jul] => 1 [May] => 1 );
*
* Notice:- June, July & May aren't in the right position.
*/
print_r($result);
}
任何帮助表示赞赏。