2013-07-18 169 views
3

我有2個陣列(可能更多),他們可能有散射數據。從多個陣列創建主陣列

Date | Apple      Date | Banana 
-----------------     ------------------- 
    1  | 5       1  | 3 
    4  | 5       3  | 1 
    5  | 5       7  | 2 

數組結構如下圖所示。

array 
(
    [0] => array 
    (
     [date] => 4 
     [apple] => 5 
    ) 
    [1] => array 
    (
     [date] => 5 
     [apple] => 5 
    ) 
) 

我需要的是有一個具有兩個細節的統一陣列。示例如下所示。

Date  Apple  Banana 
-------------------------------  
1   5   3 
3   0   1 
4   5   0 
5   5   0 
7   0   2 

考慮到數組可以容納多個值的事實,我們如何才能做到這一點?任何提示都會幫助我解決這個問題。或者,如果已經討論過,請指出我的正確鏈接。搜索期間我找不到任何東西。

在此先感謝。

我想出了
+0

的輸出是否有一個單一字段,如「日期」,其將作爲樞軸?爲給定的數據透視鍵重複鍵會發生什麼? – Makita

+0

是的。關鍵的「日期」在所有上述陣列中都是常見的。 「日期」中也不會有重複的數組。 – Purus

+0

如果您要將Date字段更改爲實際的數組鍵,那會是一個問題嗎? – Lochemage

回答

0

基於@Hristo響應,我修改了代碼,它的工作方式如下。通過這種方式,我得到了預期的產出。

下面不是我使用的確切代碼。我修改了與我在問題中發佈的示例一致。

這將工作在一個巨大的陣列?

$finalArray = array(); 

    foreach ($apple as $key => $value) { 
     $finalArray[$value['date']]['apple'] = $value['apple']; 
    } 

    foreach ($banana as $key => $value) { 
     $finalArray[$value['date']]['banana'] = $value['banana']; 
    } 

    var_dump($finalArray); 

這給出了

array 
(
    [1] => array 
    (
     [apple] => 5 
     [banana] => 3 
    ) 
) 
1

最好的事情是這樣的:

for($i=0; $i<5; $i++){ 
     $t = $i; 
     $t1 = $t+3; 
     $te= $i+5; 
     $ti = $i+10; 
     $a[] = array(
      'date' => $t, 
      'bannana' => $te, 
      'tea' => $ti, 
     ); 

     $b[] = array(
      'date' => $t1, 
      'lemon' => $te, 
     ); 

     $ar1[]=$a; 
     $ar2[]=$b; 
    } 



    foreach($a as $key=>$value){ 
     foreach($value as $k=>$v){ 
      $ar[$value['date']][$k]=$v; 
     } 
    } 

    foreach($b as $key=>$value){ 
     foreach($value as $k=>$v){ 
      $ar[$value['date']][$k]=$v; 
     } 
    } 

    echo var_dump($ar); 

其中$ a和$ b是2點的數組,並通過命令字段爲「日期」。我看着堆棧溢出,但不能完全得到這個確切的案件的另一種解決方案。

這一個產生以下的var_dump():

array(8) { 
    [0]=> array(3) { 
      ["date"]=> int(0) 
      ["bannana"]=> int(5) 
      ["tea"]=> int(10) 
    } 
    [1]=> array(3) { 
      ["date"]=> int(1) 
      ["bannana"]=> int(6) 
      ["tea"]=> int(11) 
    } 
    [2]=> array(3) { 
      ["date"]=> int(2) 
      ["bannana"]=> int(7) 
      ["tea"]=> int(12) 
    } 
    [3]=> array(4) { 
      ["date"]=> int(3) 
      ["bannana"]=> int(8) 
      ["tea"]=> int(13) 
      ["lemon"]=> int(5) 
    } 
    [4]=> array(4) { 
      ["date"]=> int(4) 
      ["bannana"]=> int(9) 
      ["tea"]=> int(14) 
      ["lemon"]=> int(6) 
    } 
    [5]=> array(2) { 
      ["date"]=> int(5) 
      ["lemon"]=> int(7) 
    } 
    [6]=> array(2) { 
      ["date"]=> int(6) 
      ["lemon"]=> int(8) 
    } 
    [7]=> array(2) { 
      ["date"]=> int(7) 
      ["lemon"]=> int(9) 
    } 
} 
+0

這給出了不同的輸出。陣列 ( [2] =>數組 ( [數] => 5 [日期] => 2 ) ) – Purus

+0

HMM的我認爲有一些我沒有得到。我會把你的整個代碼和數組創建以及結果一起發給你,告訴我有什麼不同。 –

+0

當然..同時我也嘗試基於你的解決方案。 – Purus

0

使用for循環,可以遍歷是要添加的陣列,發現在主表中現有的「日期」條目或創建新的匹配所添加數組的當前「日期」值。如果主表中的條目數據需要新的屬性類型(例如Apple,Banana),則將其添加到數據中。我也建議你使用「日期」作爲數組鍵爲好,它會更容易快速找到匹配的是這樣的:

foreach ($appleArray as $value) { 
    $date = $value['date']; 
    if (isset($masterArray[$date])) { 
    // Goes here if the date exists in the master array already. 
    // In this case, we just append to our data the fields 
    // found in the $value. 
    foreach ($value as $subKey => $subValue) { 
     // Iterate through each value, skip our 'date' key, and append 
     // each other item to our master array. 
     if ($subKey != 'date') { 
     $masterArray[$date][$subKey] = $subValue; 
     } 
    } 
    } else { 
    // Goes here if the date isn't in the master array yet. 
    // In this case, create a new entry and just copy the data. 
    $masterArray[$date] = $value; 
    } 
} 

在這個系統中,將有當一個約會條目包含丟失的字段一個蘋果,而不是香蕉等。在這些情況下,你可以假設這些值爲0,或遍歷整個列表並手動添加'banana'字段。

+0

感謝您的答案..我沒有任何主數組開始。主應該基於子數組構建..我認爲當解析第二個子數組使用相同的masterArray時,這個解決方案可能會重疊值。 – Purus

+0

我試過了,正如我所說的,主表中的值被替換爲處理第二個孩子。 – Purus