具有不同数组的键上所有值的总和

时间:2015-08-07 21:22:09

标签: php

以下是五个阵列。我们的想法是计算匹配键的所有值。并保持单一的外观。 所有这些应该出现在一个新的数组中。因此,14855应该具有值6,并且101值7等。

$arr_one = Array ( [14638] => 5 [14855] => 5 ) 
$arr_two = Array ( [101] => 4 [10141] => 4 [1015] => 4 [1020] => 4 [10353] => 4 [1048] => 4 [10582] => 4 [1060] => 4 [10675] => 4 [1068] => 4 [1084] => 4 [1098] => 4
$arr_three = Array ( [101] => 3 [10141] => 3 [602] => 3 [341] => 3 [3523] => 3 [922] => 3 [2099] => 3 [7305] => 3 [222] => 3 [537] => 3 [2792] => 3
$arr_four = Array ( [10141] => 2 [1232] => 2 [10909] => 2 [129] => 2 [155] => 2 [] => 2 [156] => 2
$arr_five = Array ( [14855] => 1 [96] => 1 [120] => 1 [129] => 1 [155] => 1 [156] => 1 

有没有办法做到这一点,并且可以选择稍后添加更多数组?

希望有人能帮我解决这个脑筋急转弯!

1 个答案:

答案 0 :(得分:1)

这是一个有挑战性的问题,但同时也是一个令人讨厌的问题;)

我很享受3小时的工作而且已经完成了。

首先要说问题中的数组格式不正确。

这个问题肯定可以通过多种方式解决。这是实现目标并演示如何实现目标的一种方法的示例,因此欢迎您使用它并了解更多信息。

我在代码中写了一些笔记。您可以扩展它以处理无限数组,但总和计算仅适用于2个键,如果您有超过2个键,我将留给您,因为现在您有了这个概念。

<?php

$arr1 = [14638 => 5, 14855 => 5];
$arr2 = [101 => 4, 10141 => 4, 1015 => 4, 1020 => 4, 10353 => 4, 1048 => 4, 10582 => 4, 1060 => 4, 10675 => 4, 1068 => 4, 1084 => 4, 1098 => 4];
$arr3 = [101 => 3, 10141 => 3, 602 => 3, 341 => 3, 3523 => 3, 922 => 3, 2099 => 3, 7305 => 3, 222 => 3, 537 => 3, 2792 => 3];
$arr4 = [10141 => 2, 1232 => 2, 10909 => 2, 129 => 2, 155 => 2, 0 => 2, 156 => 2];
$arr5 = [14855 => 1, 96 => 1, 120 => 1, 129 => 1, 155 => 1, 156 => 1];

//We merge arrays
$arrMerge = array($arr1, $arr2, $arr3, $arr4, $arr5);

//Count how many keys in merged array subtract 1 for out for loop
$arrCount = count($arrMerge) - 1;

//Our new array that contain a copy of all keys and values with out doubles
$arrNew = array();

//Creating new array of all arrays
for ($i = 0; $i < $arrCount; $i ++)
{
    foreach ($arrMerge[$i] as $key => $value)
    {
        //echo "Key: $key; Value: $value<br />\n";
        $arrNew[$key] = $value;
    }
}

//The algorithm
for ($i = 0; $i < $arrCount; $i ++)
{
    echo "<b>Array " . $i . " comparing with : </b>";
    for ($j = $arrCount; $j > $i; $j --)
    {
        echo "<br> Array " . $j . " : <br/>";

        for ($k = 0; $k < count($arrMerge[$i]); $k ++)
        {
            $key1 = array_keys($arrMerge[$i]);
            $value1 = array_values($arrMerge[$i]);
            for ($l = 0; $l < count($arrMerge[$j]); $l ++)
            {
                $key2 = array_keys($arrMerge[$j]);
                $value2 = array_values($arrMerge[$j]);
                if ($key1[$k] == $key2[$l])
                {
                    echo "match found: " . $key1[$k] . " and " . $key2[$l] . " are identical keys, ";
                    echo "we sum : " . $value1[$k] . " and " . $value2[$l] . "<br />";
                    //We update the new array with the summed values.
                    $arrNew[$key1[$k]] = $value2[$l] + $value1[$k];
                }
            }

        }
    }
    echo "<br><br>";
}

// printing results
echo "<b>This our new array with sum values</b><br/>";
foreach ($arrNew as $key => $value)
{
    echo "Key: $key; Value: $value<br />\n";
}

?>

经过测试,此处是此代码的输出,第一部分是匹配键和最后一部分的列表,您可以看到键14855得到值6而键101得到值7等。

比较所有5个阵列键输出

Array 0 comparing with :
Array 4 :
match found: 14855 and 14855 are identical keys, we sum : 5 and 1
Array 3 :
Array 2 :
Array 1 :

Array 1 comparing with :
Array 4 :    
Array 3 :
match found: 10141 and 10141 are identical keys, we sum : 4 and 2
Array 2 :
match found: 101 and 101 are identical keys, we sum : 4 and 3
match found: 10141 and 10141 are identical keys, we sum : 4 and 3

Array 2 comparing with :
Array 4 :
Array 3 :
match found: 10141 and 10141 are identical keys, we sum : 3 and 2

Array 3 comparing with :
Array 4 :
match found: 129 and 129 are identical keys, we sum : 2 and 1
match found: 155 and 155 are identical keys, we sum : 2 and 1
match found: 156 and 156 are identical keys, we sum : 2 and 1

最后一部分我们的新数组与每个键的总和

This our new array with sum values
Key: 14638; Value: 5
Key: 14855; Value: 6
Key: 101; Value: 7
Key: 10141; Value: 5
Key: 1015; Value: 4
Key: 1020; Value: 4
Key: 10353; Value: 4
Key: 1048; Value: 4
Key: 10582; Value: 4
Key: 1060; Value: 4
Key: 10675; Value: 4
Key: 1068; Value: 4
Key: 1084; Value: 4
Key: 1098; Value: 4
Key: 602; Value: 3
Key: 341; Value: 3
Key: 3523; Value: 3
Key: 922; Value: 3
Key: 2099; Value: 3
Key: 7305; Value: 3
Key: 222; Value: 3
Key: 537; Value: 3
Key: 2792; Value: 3
Key: 1232; Value: 2
Key: 10909; Value: 2
Key: 129; Value: 3
Key: 155; Value: 3
Key: 0; Value: 2
Key: 156; Value: 3