2013-06-20 4 views
0

달을 올바른 순서로 표시하려고 할 때 시퀀스의 잘못된 위치에서 6 월, 7 월 및 5 월을 가져옵니다.PHP 배열 키를 월 순서로 정렬

제대로 작동하려면 도움을 주시면 감사하겠습니다.

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

아무쪼록 부탁드립니다.

+0

은 어떻게 날짜를 얻고있다? – Orangepill

답변

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

}

0

봅니다 기능

array_key_exists(); 

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

do this for $months_date also. 

희망이 도움을 사용할 수 있습니다.

0

여전히 당신은 당신이 다음 또한 다음을 시도하려는 대답

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

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

모두에 대해이 작업을 수행을받지 못하고있다.

도움이 될만한 정보가 있습니다.

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