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

Php 具有不同数组的键上所有值的总和,php,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] =&g

下面是五个数组。其思想是计算匹配键的所有值。并保持单一的外观。 所有这些都应该出现在一个新的数组中。所以,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 
有没有一种方法可以做到这一点,并且可以选择以后添加更多阵列


希望有人能帮我做这个脑筋急转弯

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

我喜欢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";
}

?>
最后一部分是每个键的总和的新数组

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

将这些数组添加到另一个数组中。比如“total_array”。现在推一推你在里面的那个人。然后,找到“total_array”的长度,创建一个循环,然后为单个数组创建另一个循环,查找每个数组的长度,然后对值进行解析。同时,在另一个数组中不断添加这些值,比如“check_key”。现在,对于“total_array”中的每个键,搜索check_键的内容,如果键匹配,则添加新的值。但似乎无法找到合适的代码。所有计数的值都会出现错误和奇怪的值。也许任何人都可以输入一些代码来解决这个问题?你太棒了!非常感谢你所做的一切!这是非常好的和符合逻辑的,足以让我从中学到很多东西。我已经测试并让代码运行了。谢谢我的朋友!
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