2012-07-18 109 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