0

我试图让我的函数以正确的顺序显示月份,它在序列中的错误位置获得六月、七月和五月。

我非常感谢一些帮助,以使其正常工作。

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);
}

任何帮助表示赞赏。

4

4 回答 4

0

尝试使用函数

array_key_exists();

i.e: $months_name = array_key_exists('jan','feb'...)

do this for $months_date also.

希望这有帮助。

于 2013-06-20T04:53:22.397 回答
0

你仍然没有得到你想要的答案然后也尝试以下一个,

after array_key_exists() just unset() both the $months_name & $months_date

i.e: unset($months_name['your month_name value here'])

对两者都这样做。

那可能对你有帮助。

于 2013-06-20T04:59:10.950 回答
0
<?php

$all_incidents[]["incident_date"]="02-02-2013";
$all_incidents[]["incident_date"]="02-03-2013";
$all_incidents[]["incident_date"]="02-01-2013";

    $months=array(1=>"Jan",2=>"Feb",3=>'Mar',4=>'Apr',5=>'May',6=>'Jun',7=>'Jul',8=>'Aug',9=>'Sep',10=>'Oct',11=>'Nov',12=>'Dec'); 

    foreach($all_incidents as $key => $value)
    {
        $date_bits = explode('-', $value['incident_date']);
        $incidentMonth=intval($date_bits[1]); 
        if($incidentMonth>0)
        {
            $incidents[$incidentMonth]++;
        }         
    }

    ksort($incidents);
    foreach($incidents as $m=>$number)
    {
     echo "Month:".$months[$m]." , Incidents:".$number."<br>";
    }
?>

演示

于 2013-06-20T04:59:36.310 回答
0

不保证关联数组中元素的顺序。您正在用文本版本替换数字月份,然后对其进行累积。您可以 extract $result['May'],但不能保证它是数组的第五个元素。

我建议您累积数字月份,并在末尾添加文本月份。

这是一个可以做你想做的功能:

公共函数 get_incident_data() { events_m->get_all_by_date();

// short array for proof-of-concept.
$all_incidents = array(array("incident_date"=>"01-01-2013"),
                      array("incident_date"=>"01-06-2013"),
                       array("incident_date"=>"01-05-2013"),
                       array("incident_date"=>"01-10-2013")
                      );

// 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  of incidents, extract each numerical month and coerce to an integer.
* Accumulate in the $result array, prefilled with zeros.
*/
$result = array_fill(1,12,0);
foreach($all_incidents as $value)
{
    $date_bits = explode('-', $value['incident_date']);
    $numericalMonth = intval($date_bits[1],10);
if ($numericalMonth != 0) {
  $result[$numericalMonth]++;
}
}

// Add the text months as keys. 
$result = array_combine($months_name, $result);

print_r($result);?>

}

于 2013-06-20T04:43:56.887 回答