2011-06-08 122 views
-1

我有一個多維數組的形式;整理多維數組值

Array 
(
    [0] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Bob Hope 
      [ITEM] => Holiday 
      [DURATION] => 04:00 
     ) 
    [1] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Bob Hope 
      [ITEM] => Work 
      [DURATION] => 05:00 
     ) 
    [2] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Bob Hope 
      [ITEM] => Holiday 
      [DURATION] => 06:00 
     ) 
    [3] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Bob Hope 
      [ITEM] => Work 
      [DURATION] => 05:00 
     ) 
    [4] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Joe Bloggs 
      [ITEM] => Holiday 
      [DURATION] => 02:00 
     ) 
    [5] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Joe Bloggs 
      [ITEM] => Work 
      [DURATION] => 03:00 
     ) 
    [6] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Joe Bloggs 
      [ITEM] => Holiday 
      [DURATION] => 03:00 
     ) 
    [7] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Joe Bloggs 
      [ITEM] => Work 
      [DURATION] => 02:00 
     ) 
) 

想整理結果,總結每個人的假期和工作,以便我可以輸出一些東西的形式;

Array 
(
    [0] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Bob Hope 
      [ITEM] => Holiday 
      [DURATION] => 10:00 
     ) 
    [1] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Bob Hope 
      [ITEM] => Work 
      [DURATION] => 10:00 
     ) 
    [2] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Joe Bloggs 
      [ITEM] => Holiday 
      [DURATION] => 05:00 
     ) 
    [3] => Array 
     (
      [DATE] => 05-19-2011 
      [PERSON] => Joe Bloggs 
      [ITEM] => Work 
      [DURATION] => 05:00 
     ) 
) 

任何想法?任何和所有幫助表示讚賞。

+1

有什麼區別? – jantimon 2011-06-08 08:22:56

+0

他將他們分組Bob Hope的假期合併,第二個例子的DURATION爲10。 – tradyblix 2011-06-08 08:24:37

+0

@Ghommey [DURATION]加起來就是[ITEM]的唯一條目,而[NAME] – 2011-06-08 08:25:40

回答

1

好,很多的foreach的,但你基本上是添加在一個單獨的結構中的值,並重新排列結果數組:

$items = array(
    0 => Array(
     'DATE' => '05-19-2011', 
     'PERSON' => 'Bob Hope', 
     'ITEM' => 'Holiday', 
     'DURATION' => '10:00', 
    ), 
    1 => Array(
      'DATE' => '05-19-2011', 
      'PERSON' => 'Bob Hope', 
      'ITEM' => 'Work', 
      'DURATION' => '10:00', 
    ), 
    2 => Array(
      'DATE' => '05-19-2011', 
      'PERSON' => 'Joe Bloggs', 
      'ITEM' => 'Holiday', 
      'DURATION' => '05:00', 
    ), 
    3 => Array(
      'DATE' => '05-19-2011', 
      'PERSON' => 'Joe Bloggs', 
      'ITEM' => 'Work', 
      'DURATION' => '05:00', 
    ), 
); 

foreach($items as $key=>$value){ 
    $item = $value['ITEM']; 
    $out[$value['PERSON']][$value['DATE']][$value['ITEM']] += $value['DURATION']; 
} 

foreach($out as $person=>$data){ 
    foreach($data as $date=>$items){ 
     foreach($items as $item=>$duration){ 
      $result[] = array(
       'PERSON'=>$person, 
       'DATE'=>$date, 
       'ITEM'=>$item, 
       'DURATION'=>$duration, 
      ); 
     } 
    } 
} 
print_r($result); 
0
  • 聲明一個新的數組。
  • 遍歷舊數組
  • 如果新的數組爲空,當前數組元素添加進去
  • 如果沒有,迭代新陣列
    • ,如果沒有新的數組元素的電流匹配數組元素,添加它
    • 否則當前元素的期限增加了有關新的數組元素
2

在哪裏數據來自哪裏?一般情況下,這樣的數據來自數據庫,在這種情況下,下面的語句會做你想要什麼:

SELECT 
    SUM(DURATION), 
    ITEM, 
    PERSON 
FROM 
    yourtable 
GROUP BY 
    PERSON, 
    ITEM; 

編輯:如果你想通過他們的項目組也一樣,只是聲明。

+0

我通常會同意你的意見,但這是我的問題的簡化版本。我的問題是關於值的分組和集合。但正如你所說的是正確的,我已經投了答案。 – Rooneyl 2011-06-08 08:38:34

+0

謝謝。那麼,如果它*來自數據庫,那麼你可以用SQL比用PHP更簡單地做任何你想做的事情。 – 2011-06-08 09:01:19

+1

在這種情況下,我也會推薦使用SQL,因爲在php中,事情會變得非常醜陋,這不僅僅是因爲你必須解析持續時間到秒數來總結它們。如果你使用mysql,有一個函數用於這個順便說一句:TIME_TO_SEC('00:00:00'),你可以總結它漿果顯示。我相信你可以將TIME_TO_SEC的持續時間格式化,比在php中爲此構建解決方案要容易得多。 – aurora 2011-06-08 09:12:09

0

如果你真的想用PHP我建議不解決這個嵌套太多循環。這是我的解決方案(它看起來相當醜陋btw。):

$result = array(); 
foreach ($data as $row) { 
    $key = $row['PERSON'] . '|' . $row['ITEM']; 
    $tmp = explode(':', $row['DURATION']); 
    $row['DURATION'] = $tmp[0] * 60 + $tmp[1]; 

    if (!isset($result[$key])) { 
     $result[$key] = $row; 
    } else { 
     $result[$key]['DURATION'] += $row['DURATION']; 
    } 
} 

$result = array_map(function($v) { 
    $v['DURATION'] = strftime('%M:%S', $v['DURATION']); 

    return $v; 
}, array_values($result));