2013-07-18 99 views
0

如何將這些數組合並在一起?PHP - 按鍵合併2D數組

Array 
    (
     [0] => Array 
      (
       [type] => Person 
       [relevance] => 0.700000 
       [count] => 300 
       [text] => Chris 
      ) 
    ) 
    Array 
    (
     [0] => Array 
      (
       [type] => Person 
       [relevance] => 0.900000 
       [count] => 400 
       [text] => Chris 
      ) 

     [1] => Array 
      (
       [type] => Person 
       [relevance] => 0.500000 
       [count] => 200 
       [text] => Tom 
      ) 
    ) 

或陣列是這樣的:

Array 
     (
      [0] => Array 
       (
        [type] => Person 
        [relevance] => 0.700000 
        [count] => 300 
        [text] => Chris 
       ) 


      [1] => Array 
       (
        [type] => Person 
        [relevance] => 0.900000 
        [count] => 400 
        [text] => Chris 
       ) 

      [2] => Array 
       (
        [type] => Person 
        [relevance] => 0.500000 
        [count] => 200 
        [text] => Tom 
       ) 
     ) 

預期的結果是:

Array 
(
    [0] => Array 
     (
      [type] => Person 
      [relevance] => 0.800000 
      [count] => 700 
      [text] => Chris 
     ) 



    [1] => Array 
     (
      [type] => Person 
      [relevance] => 0.500000 
      [count] => 200 
      [text] => Tom 
     ) 
) 

[相關性]值是平均值數

[COUNT]值是增量號碼

這些數組的合併應基於[文本]值。 我該如何用快速的方法來做到這一點?

感謝您的幫助。

回答

2

如果是這樣的陣列

$array = Array(
    Array(
     'type' => 'Person', 
     'relevance' => .7, 
     'count' => 300, 
     'text' => 'Chris' 
    ), 
    Array(
     'type' => 'Person', 
     'relevance' => .9, 
     'count' => 400, 
     'text' => 'Chris' 
    ), 
    Array(
     'type' => 'Person', 
     'relevance' => .5, 
     'count' => 200, 
     'text' => 'Tom' 
    ), 
); 

則:

$tmp = Array(); 

foreach($array as $obj) { 
    if(!isset($tmp[$obj['text']])) { 
     $tmp[$obj['text']] = array_merge(Array('total_count'=>1),$obj); 
     continue; 
    } 
    $tmp[$obj['text']]['count']  += $obj['count']; 
    $tmp[$obj['text']]['relevance'] += $obj['relevance']; 
    $tmp[$obj['text']]['total_count']++; // useful for average calculation 
} 

$result = Array(); 

foreach($tmp as $key=>$obj) { 
    $obj['relevance'] = $obj['relevance']/$obj['total_count']; 
    unset($obj['total_count']); // useless now 
    $result[] = $obj; 
} 

print_r($result); 
+0

我喜歡你的平均方法比我的更好,+1 –

+0

謝謝......但我認爲還有另一種更快的解決方案:-)我在等待其他答案。 –

+0

@chumkiu我試過你的答案,它完美的作品。謝謝 –

2

事情是這樣的,也許:

$newArray = array(); 

foreach ($oldArray as $item) { 
    if (!array_key_exists($item['text'], $newArray)) { 
     $newArray[$item['text']] = $item; 
     $newArray[$item['relevance_values']] = array(); 

    } else { 
     $newArray[$item['text']]['count'] += $item['count']; 
     $newArray[$item['relevance_values']][] = $item['relevance']; 
    } 
} 

foreach ($newArray as $item) { 
    $relevance = 0; 
    foreach ($item['relevance_values'] as $value) { 
     $relevance += $value; 
    } 
    $item['relevance'] = $relevance/count($item['relevance_values']); 
} 
+0

他確實要求在'newArray'上的鍵是數字。 –

+0

@JohnVanDeWeghe他可以使用'array_values($ newArray)'來獲得一個索引數組。 –

1

試試這個:

function mergeOnText($array){ 
    $results = array(); 
    foreach($array as $person){ 
     $found = -1; 
     foreach($results as $i => $res) 
      if($res['text'] == $person['text']){ 
       $found = $i; 
       break; 
      } 
     if($found > -1){ 
      $results[$found]['relevance'][] = $person['relevance']; 
      $results[$found]['count'] += $person['count']; 
     } else { 
      $results[] = $person; 
     } 
    } 
    foreach($results as $i => $res) 
     $results[$i]['relevance'] = array_sum($res['relevance'])/count($res['relevance']); 
    return $results; 
}