2015-08-28 31 views
2

大家好,我是一個PHP新手。我嘗試了很多事情來做到這一點,但我不能得到所需的輸出。 這是我的陣列我想對數組進行排序並計算數組中的月份數,並根據月份將所有數據推送到新數組後計數數組中的數據。數據計數,密鑰對。我想根據日期明智排序多維數組,並計算數組中出現的數據的數量

Array 
(
    [0] => Array 
     (
      [comp_date] => 2015/07/23 06:20 
      [comp_status] => Lead 
     ) 

    [1] => Array 
     (
      [comp_date] => 2015/07/23 06:20 
      [comp_status] => Lead 
     ) 

    [2] => Array 
     (
      [comp_date] => 2015/07/23 06:20 
      [comp_status] => Opportunity 
     ) 

    [3] => Array 
     (
      [comp_date] => 2015/07/24 06:20 
      [comp_status] => Conversion 
     ) 

    [4] => Array 
     (
      [comp_date] => 2015/07/24 06:20 
      [comp_status] => Lead 
     ) 

    [5] => Array 
     (
      [comp_date] => 2015/08/24 06:20 
      [comp_status] => Opportunity 
     ) 

    [6] => Array 
     (
      [comp_date] => 2015/08/24 06:20 
      [comp_status] => Conversion 
     ) 

    [7] => Array 
     (
      [comp_date] => 2015/07/25 06:20 
      [comp_status] => Lead 
     ) 

    [8] => Array 
     (
      [comp_date] => 2015/06/25 06:20 
      [comp_status] => Opportunity 
     ) 

    [9] => Array 
     (
      [comp_date] => 2015/08/25 06:20 
      [comp_status] => Conversion 
     ) 

    [10] => Array 
     (
      [comp_date] => 2015/08/25 06:20 
      [comp_status] => Lead 
     ) 
) 

這是我的實際數組。 我已經厭倦了這個

function yearWiseCalculation(){ 
$query = mysql_query("SELECT comp_date,comp_status FROM `hr_companies` WHERE year(comp_date)=year(curdate())"); 
$arr =[]; 
while ($total = mysql_fetch_assoc($query)) { 

    array_push($arr,$total); 
    $ex = explode(" ", $total['comp_date']); 
    $ex = explode(" ", $ex[0]); 
    $k = explode("/", $ex[0]); 
    $time[]=strtotime($ex[0]); 
    $month[]=date("F",$time[0]); 
} 

我想以這種形式輸出給出如下

Array(
     [0] => Array 
     (
      [comp_month] => july 
      [lead] => 4 
      [opportunity] => 1 
      [conversion] => 1 
     ) 
     [1] => Array 
     (
      [comp_month] => August 
      [lead] => 1 
      [opportunity] => 1 
      [conversion] => 2 
     ) 

) 
+0

我第一次嘗試根據這之後,我從日期取個月,而爆炸數組的第一個元素,並將其推入新的數組,但我無法計數的月數日的數據進行排序發生在一個數組中,我不能將它們分開,也不能根據月份對數據進行計數。 –

回答

1

您可以簡單地使用foreach作爲

$result = []; 
foreach ($arr as $key => $value) { 
    $hash = date('F', strtotime($value['comp_date'])); 
    if (isset($result[$hash])) { 
     $result[$hash]['comp_month'] = $hash; 
     $result[$hash]['lead'] = (isset($value['comp_status']) && $hash == $result[$hash]['comp_month'] && $value['comp_status'] == 'Lead') ? $result[$hash]['lead'] + 1 : $result[$hash]['lead']; 
     $result[$hash]['opportunity'] = (isset($value['comp_status']) && $hash == $result[$hash]['comp_month'] && $value['comp_status'] == 'Opportunity') ? $result[$hash]['opportunity'] + 1 : $result[$hash]['opportunity']; 
     $result[$hash]['conversion'] = (isset($value['comp_status']) && $hash == $result[$hash]['comp_month'] && $value['comp_status'] == 'Conversion') ? $result[$hash]['conversion'] + 1 : $result[$hash]['conversion']; 
    } else { 
     $result[$hash]['comp_month'] = date('F', strtotime($value['comp_date'])); 
     $result[$hash]['lead'] = ($value['comp_status'] == 'Lead') ? 1 : 0; 
     $result[$hash]['opportunity'] = ($value['comp_status'] == 'Opportunity') ? 1 : 0; 
     $result[$hash]['conversion'] = ($value['comp_status'] == 'Conversion') ? 1 : 0; 
    } 
} 
uksort($result,function($a,$b){ return strtotime($a) - strtotime($b);}); 
print_r(array_values($result)); 

Demo

1

這裏是一個foreach() + array_walk()版本:

foreach($array as $key => $row) { 
    $dateKey    = date("Y-m",strtotime(str_replace('/',"-",$row['comp_date']).':00')); 
    $new[$dateKey][]  = strtolower($row['comp_status']); 
    $count[$dateKey]  = array_count_values($new[$dateKey]); 
} 

array_walk($count,function(&$count,$k) { 
    $count['comp_date']  = date("F",strtotime($k)); 
    $count['conversion'] = (empty($count['conversion']))? '0':$count['conversion']; 
    $count['lead']   = (empty($count['lead']))? '0':$count['lead']; 
    $count['opportunity'] = (empty($count['opportunity']))? '0':$count['opportunity']; 
    ksort($count); 
}); 

ksort($count,SORT_NATURAL); 
print_r(array_values($count));