2014-12-01 62 views
0

我有一個多維數組,並且如果它們具有相同的division_id但不同的post_page_id並且返回一個數組,其中包含每個分區的總值並取消其餘設置,則會嘗試合計觀衆總數。我試過雙foreach但它沒有工作。任何幫助?由於取消設置多維數組中的數組php

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [post_page_id] => 22130 
      [audience] => 276 
      [type] => facebook 
      [division_id] => 1 
      [tw_audience] => 
     ) 

    [1] => Array 
     (
      [id] => 14 
      [post_page_id] => 22465 
      [audience] => 6 
      [type] => facebook 
      [division_id] => 1 
      [tw_audience] => 
     ) 

    [2] => Array 
     (
      [id] => 2 
      [post_page_id] => 22189 
      [audience] => 175 
      [type] => twitter 
      [division_id] => 2 
      [tw_audience] => 
     ) 
    [3] => Array 
     (
      [id] => 1 
      [post_page_id] => 23044 
      [audience] => 180 
      [type] => facebook 
      [division_id] => 2 
      [tw_audience] => 
     ) 
) 

所以我想輸出是這樣

Array 
(
    [0] => Array 
     (
      [id] => 1 
      [post_page_id] => 22130, 22465 
      [audience] => 282 
      [type] => facebook 
      [division_id] => 1 
      [tw_audience] => 
     ) 

    [1] => Array 
     (
      [id] => 2 
      [post_page_id] => 22189, 23044 
      [audience] => 180 
      [type] => twitter+facebook 
      [division_id] => 2 
      [tw_audience] => 175 
     ) 

) 
+0

你能提供一個你想要結果看起來像什麼樣子的例子嗎? – ilikesleeping 2014-12-01 23:14:32

+0

剛剛編輯,基本上,我想合併數組,並總結觀衆,並取消合併。謝謝 – vic 2014-12-01 23:31:53

回答

0

聯想陣列是你的朋友。

$result = array(); 
foreach ($input as $inp) 
{ 
    $idx = $inp ['division_id']; 
    if (isset ($result [$idx])) // New result, just copy it across 
    { 
     $result [$idx] = $inp; 
     // Since we are going to have multiple pages, turn the page_id into an array 
     $result [$idx]['post_page_id'] = array ($inp ['post_page_id']); 
    } 
    else // Already exists - add the totals to the existing one 
    { 
     $result [$idx]['audience'] += $inp ['audience']; 
     $result [$idx]['post_page_id'][] = $inp ['post_page_id']; 
    } 
} 
// Now, I assume you want to get rid of duplicates 
foreach ($result as &$r) 
{ 
    // &$r means you are working with a pointer to the original, not a copy 
    // so changes are applied back to the original 
    $r ['post_page_id'] = array_unique [$r ['post_page_id']]; 
    // and if you really want the pages in comma-delimited format... 
    $r ['post_page_id'] = implode (', ', $r ['post_page_id']); 
} 

您可能想要對post_page_id的源執行相同的操作,但會使用'+'將它們分解。

+1

管理單元!雖然你的第五行可能應該是'if(!isset ...' – ilikesleeping 2014-12-02 00:33:41

+0

謝謝你,這很好。 – vic 2014-12-02 01:31:34

+0

哎呀,你說得對! – Mike 2014-12-02 01:59:05

0

最好的辦法是不要取消設置當前數組中的元素,而是寫一個新的,合併到你走。在迭代時修改數組的結構通常是一個糟糕的主意。

如果要合併division_id,可以使用該值作爲數組鍵來寫入新數組。然後檢查它是否已經存在 - 如果不存在,則複製該行,如果存在,則合併要合併的值。

假設你原來的數組是在一個名爲$originalArray變量:

$newArray = array(); 
foreach ($originalArray as $row) { 
    $divID = $row['division_id']; 
    // Check if this division_id has occurred before 
    if (!isset($newArray[$divID])) { 
     // Add in the first row if not 
     $newArray[$divID] = $row; 
    } else { 
     // Otherwise, merge it into existing row 

     // Merge audience 
     $newArray[$divID]['audience'] += $row['audience']; 

     // Merge post IDs 
     $newArray[$divID]['post_page_id'] .= ', ' . $row['post_page_id']; 

     // Merge type - need to account for duplicates 
     $typeVals = explode('+', $newArray[$divID]['type']); 
     if (!in_array($row['type'], $typeVals)) { 
      $newArray[$divID]['type'] .= '+' . $row['type']; 
     } 
    } 

} 

// Overwrite original array, and use array_values() to reset keys if needed 
$originalArray = array_values($newArray); 
unset($newArray, $typeVals); 

您可能需要細化根據確切的數據類型的合併方法,但你應該明白我的意思。