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/>";
}
4

2 回答 2

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 将合并它想要添加数据的数据

于 2012-07-18T11:58:24.993 回答
2

您可以通过简单地保持一个由要合并的键组织的全局数组来做到这一点,并且您将避免不得不以 O(n²) 复杂性来完成它,只需简单的 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));

希望有帮助!

于 2012-07-18T11:59:52.970 回答