2012-07-18 200 views
1

我想在PHP中比較和合並數組。可以說我有兩個數組如下比較和合並陣列

$topCountries1 = array(
    array(
     'India', 
     '23', 
     '34', 
     '11'), 
    array(
     'USA', 
     '13', 
     '24', 
     '21'), 
    array(
     'Japan', 
     '13', 
     '24', 
     '21')); 

$topCountries2 = array(
    array(
     'France', 
     '23', 
     '34', 
     '11'), 
    array(
     'India', 
     '13', 
     '24', 
     '21'), 
    array(
     'Japan', 
     '13', 
     '24', 
     '21')); 

我想上面的兩個數組合並,這樣我會有一套獨特的國家,如果有重複的國家在數組中應該添加的值的值其他三個領域並結合起來。

嘗試下面的代碼 - 但我對邏輯感到困惑。

$topCountries = array_merge($topCountries1, $topCountries2); 

$collect = array(); 

foreach ($topCountries as $tc) { 
    echo $count = count($collect); 
    if ($count > 0) { 
     foreach ($collect as $c) { 
      if ($c[0] == $tc[0]) { 
       echo "match<br/>"; 
       $collect[] = $tc; 
      } else { 
       $collect[] = $tc; 
       echo "no match<br/>"; 
      } 
     } 
    } else { 
     $collect[] = $tc; 
    } 
    echo "<br/>"; 
} 

回答

2

您可以通過簡單地保持你所要合併的關鍵組織了全局數組做到這一點,你會避免爲O做到這一點( n 2)的複雜性,只是簡單的O(n)假設密鑰的哈希查找是O(1)。

這是一個比以前發佈的解決方案更簡單的解決方案,並且接受任意數量的輸入數組而不必添加更多的代碼,此外還允許您擴展國名後的值數量,而無需添加更多代碼。

$topCountries1 = array(
    array(
     'India', 
     '23', 
     '34', 
     '11', 
    ), 
    array(
     'USA', 
     '13', 
     '24', 
     '21', 
    ), 
    array(
     'Japan', 
     '13', 
     '24', 
     '21', 
    ), 
); 

$topCountries2 = array(
    array(
     'France', 
     '23', 
     '34', 
     '11', 
    ), 
    array(
     'India', 
     '13', 
     '24', 
     '21', 
    ), 
    array(
     'Japan', 
     '13', 
     '24', 
     '21', 
    ), 
); 

$countries = array(); 
$data = array($topCountries1, $topCountries2); 

foreach($data as $entries) 
{ 
    foreach($entries as $country) 
    { 
     $name = $country[0]; 

     // if first time we see the country, add it to the list 
     if (!isset($countries[$name])) 
     { 
      $countries[$name] = $country; 
     } 
     else 
     { 
      // add all fields after the first (the name) 
      foreach (array_slice($country, 1, null, true) as $idx => $value) 
      { 
       $countries[$name][$idx] += $value; 
      } 
     } 
    } 
} 

var_dump($countries); 
var_dump(array_values($countries)); 

希望有幫助!

+0

非常感謝。它像魅力一樣工作。現在我得到了我做錯了什麼,並使整個過程變得複雜 – 2012-07-18 12:10:13

+0

因爲我有一個像下面這樣的文本鍵值的數組。 (INT)0 =>對象(stdClass的){ 國家> '印度' 訪問=> '12' newVisits => '9' percentNewVisits =>(INT)75 }, – 2012-07-18 12:10:43

+0

我還可以使用相同的?因爲我們將它用作關鍵值對? – 2012-07-18 12:11:08

2

我以不同的方式嘗試了您的答案,並導致您想要的結果。

$topCountries1 = array(
    array(
     'India', 
     '23', 
     '34', 
     '11'), 
    array(
     'USA', 
     '13', 
     '24', 
     '21'), 
    array(
     'Japan', 
     '13', 
     '24', 
     '21')); 

$topCountries2 = array(
    array(
     'France', 
     '23', 
     '34', 
     '11'), 
    array(
     'India', 
     '13', 
     '24', 
     '21'), 
    array(
     'Japan', 
     '13', 
     '24', 
     '21')); 


$collection = array(); 

foreach ($topCountries1 as $tc1) { 
    foreach($topCountries2 as $tc2){ 
     if(in_array($tc1[0], $tc2)){ 
      $collect[0] = $tc1[0]; 
      $collect[1] = $tc1[1] + $tc2[1]; 
      $collect[2] = $tc1[2] + $tc2[2]; 
      $collect[3] = $tc1[3] + $tc2[3]; 
      array_push($collection, $collect); 
     } 
    } 
} 
$final_array = $collection; 
foreach($topCountries1 as $tc1){ 
    $flag = true; 
    foreach($collection as $coll){ 
     if(in_array($tc1[0], $coll)){ 
      $flag = false; 
      break; 
     } 
    } 
    if($flag){ 
     array_push($final_array, $tc1); 
    } 
} 
foreach($topCountries2 as $tc1){ 
    $flag = true; 
    foreach($collection as $coll){ 
     if(in_array($tc1[0], $coll)){ 
      $flag = false; 
      break; 
     } 
    } 
    if($flag){ 
     array_push($final_array, $tc1); 
    } 
} 
var_dump($final_array); 

array_merge將合併數據是要添加數據

+0

非常感謝Poonam。該死的我忘了array_push:P – 2012-07-18 12:12:19