2012-12-02 162 views
1

我有表示由日期分組的數據的計數3個陣列$dummyArray1 = array('year' => array('month' => 'count'));PHP數組合並

$dummyArray1 = array(
    '2012' => array(
     '1' => 13, 
     '2' => 22, 
     '3' => 18, 
     '5' => 19 
    ), 
    '2011' => array(
     '1' => '4', 
     '3' => 13 
    ) 
); 

$dummyArray2 = array(
    '2012' => array(
     '1' => 44, 
     '4' => 34, 
     '5' => 38, 
     '6' => 29 
    ), 
    '2011' => array(
     '1' => 9, 
     '2' => 11, 
     '3' => 1 
    ) 
); 

$dummyArray3 = array(
    '2012' => array(
     '2' => 5, 
     '4' => 3, 
     '6' => 1, 
     '8' => 11 
    ), 
    '2011' => array(
     '3' => 9, 
     '4' => 14, 
     '9' => 9 
    ) 
); 

我需要組中的那些1個陣列,使得每個月份我將每個陣列的count值。如果計數數據在特定月份中不存在,則應該插入0作爲count值。

+0

爲清楚起見,您可以加入一個例子你想要的輸出?謝謝。 – Wiseguy

回答

1

是easer如果你犯了一個功能:

<?php 

function groupMyArray($dummysArray) 
{ 
    $result = array(); 
    foreach($dummysArray as $dummyArray): 
     foreach($dummyArray as $year => $countarray): 
      for($i = 1; $i <= 12; $i++): 
       if(count($countarray[$i]) > 0){ 
        $result[$year] = array($i => $countarray[$i]); 
       }else{ 
        $result[$year] = array($i => 0); 
       } 
      endfor; 
     endforeach; 
    endforeach; 

    return $result; 
} 

OK,知道你要構建$ dummyArray:

$dummyArray = array(
    $dummyArray1, 
    $dummyArray2, 
    $dummyArray3 
); 


$dummyArraygroup = groupMyArray($dummyArray); 

>

+0

所以,我沒有測試這個代碼,但理念是這樣的。 – jkamizato