2015-12-22 100 views
-1

我有一個多維數組這有點兒像這樣從多維數組創建獨特的數組?

Array 
(
    [0] => Array 
     (
      [id] => 140309 
      [headline] => Random title 
      [body] => 
      [title_generic] => 
      [text_generic] => 
     ) 

    [1] => Array 
     (
      [id] => 140309 
      [headline] => Random title 
      [body] => 
      [title_generic] => 
      [text_generic] => 
     ) 

    [2] => Array 
     (
      [id] => 140309 
      [headline] => Random title 
      [body] => 
      [title_generic] => 
      [text_generic] => 
     ) 

    [3] => Array 
     (
      [id] => 140309 
      [headline] => Random title 
      [body] => 
      [title_generic] => 
      [text_generic] => 
     ) 

    [4] => Array 
     (
      [id] => 140309 
      [headline] => Random title 
      [body] => 
      [title_generic] => Random title 
      [text_generic] => [b]This is Random title:[/b] 16 nov 2012 
     ) 

    [5] => Array 
     (
      [id] => 140309 
      [headline] => Random title 
      [body] => 
Some text goes here. Blaaaa 
      [title_generic] => 
      [text_generic] => 
     ) 

    [6] => Array 
     (
      [id] => 140309 
      [headline] => Random title 
      [body] => 
      [title_generic] => 
      [text_generic] => 
     ) 

) 

我試着用array_unique()過濾,但只返回

Array 
    (
     [0] => Array 
      (
       [id] => 140309 
       [headline] => Random title 
       [body] => 
       [title_generic] => 
       [text_generic] => 
      ) 
    ) 

但我想有

Array 
    (
     [0] => Array 
      (
       [id] => 140309 
       [headline] => Random title 
       [body] => 
       Some text goes here. Blaaaa 
       [title_generic] => Random title 
       [text_generic] => [b]This is Random title:[/b] 16 nov 2012 
      ) 
    ) 

即只返回全部填充的唯一字段。

在陣列中將只有一個唯一的填充空間,所以沒有辦法,在第一把鑰匙我會有title_generic然後我會有不同的三分之一左右。 bodytext_generic也是如此。它們在某些數組中只出現一次。但是ID,標題等都是一樣的(裏面有一個日期等等)。

有沒有一個功能會做這樣的事情?

編輯

我可能不太清楚。我想返回包含來自其他鍵(來自該鍵的數組中的值)的所有信息的數組,這些不同。所以在陣列中的前4個鍵中,我有與id,headline,body,title_generictext_generic相同的陣列。他們有相同的標識和標題,其餘都是空的。然後在下一個鍵中填入title_generictext_generic,依此類推。

我需要將填補鍵,如

Array 
    (
     [0] => Array 
      (
       [id] => 140309 
       [headline] => Random title 
       [body] => 
       Some text goes here. Blaaaa 
       [title_generic] => Random title 
       [text_generic] => [b]This is Random title:[/b] 16 nov 2012 
      ) 
    ) 

Array 
(
    [id] => 140309 
    [headline] => Random title 
    [body] => 
    Some text goes here. Blaaaa 
    [title_generic] => Random title 
    [text_generic] => [b]This is Random title:[/b] 16 nov 2012 
) 

任一陣列,我不知道該如何解釋這更好的...

+1

對不起,你能用簡單的術語再次解釋邏輯嗎?你想通過某個定義返回一個「唯一」的數組嗎?或者你想將所有這些數組合併成一個字段的值由某個「唯一性」標準選擇的數組?不,沒有內置的功能。 – deceze

+1

嗯,這取決於。如果邏輯是附加信息(例如,'title_generic','text_generic'等)不會導致衝突(多個不同的'title_generic' ...),你可以這樣做。別的,你會如何解決衝突?例如哪一個標題是正確的? –

+0

聽起來像你想將所有信息合併到一個數組中。你必須解釋如何決定選擇哪一條信息,然後是否有多個選項(如@ F.M所說的「衝突解決方案」)。 – deceze

回答

2
$result = array_reduce($array, function (array $result, array $item) { 
    return array_filter($result) + $item; 
}, []); 

這可能會做你想做的事情(這有點不清楚)。

說明:它逐個檢查您的每個項目;它會過濾所有空值,只留下已填充的密鑰(array_filter);然後它將所有不存在的密鑰(+從下一個項目添加到它(在array_reduce上閱讀)。最終結果應該是一個數組,其中所有數組中的所有非空鍵合併爲一個,並且該值是循環內遇到的第一個非空值。

+0

這與'array_unique($ array,SORT_REGULAR);'我只需要用'array()'替換'[]',它工作,有點兒。我會爲此工作。謝謝 –

1

我知道已經有一個解決方案。儘管如此,我想爲您呈現一個輕量級的方法,其執行時間和功能相同。

# your data here 
$array = [ 
    [ 
     'id' => '123', 
     'headline' => 'one two three', 
     'body' => 'somebody', 
     'title_generic' => '', 
     'text_generic' => '', 
    ], 
    [ 
     'id' => '123', 
     'headline' => 'one two three', 
     'body' => null, 
     'title_generic' => 'title', 
     'text_generic' => 'text', 
    ], 
]; 

# the aggregate to be created 
$aggregate = []; 

foreach ($array as $el) { 
    if (empty($el)) continue; 

    foreach ($el as $k => $v) { 
     if (empty($v)) continue; 

     if (!isset($aggregate[$k])) { 
      $aggregate[$k] = $v; 
     } 
    } 
} 

# debug print 
echo '<pre>';print_r($aggregate);echo '<pre>'; 

# the output 
Array 
(
    [id] => 123 
    [headline] => one two three 
    [body] => somebody 
    [title_generic] => title 
    [text_generic] => text 
) 
+0

感謝您的幫助,但事實證明,我將不得不以完全不同的方式處理這個問題(我忽略了我的陣列中有不同ID的事實:S) –

+0

@dingo_d你需要進一步的幫助嗎?或者你能解決它? –

+0

我設法解決我的問題,我的sql查詢生成這個數組,所以我現在很好:)謝謝 –