按月顺序排序PHP数组键

时间:2013-06-20 04:30:36

标签: php codeigniter

我试图让我的功能以正确的顺序显示月份,它会在序列中的错误位置显示六月,七月和五月。

我真的很感激能帮助你正常工作。

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 个答案:

答案 0 :(得分:0)

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

我建议你在数月内积累,并在最后添加文本数月。

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

公共函数get_incident_data() {     incidents_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);?>

}

答案 1 :(得分:0)

尝试使用功能

array_key_exists();

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

do this for $months_date also.

希望得到这个帮助。

答案 2 :(得分:0)

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

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

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

为两者做这件事。

这可能对你有帮助。

答案 3 :(得分: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>";
    }
?>

Demo