简体   繁体   English

基于另一个数组中相似值的数组值总和

[英]Sum of array values based on similar values from another array

This might be a little confusing, but I am going to explain it as best as I can. 这可能有点令人困惑,但是我将尽我所能解释。 Please bear with me. 请多多包涵。

I have the following arrays: 我有以下数组:

Array
(
    [question1] => 69
    [question2] => 36
    [question3] => 57
    [question4] => 69
    [question5] => 58
    [question6] => 40
    [question7] => 58
)

Array
(
    [question1] => 8
    [question2] => 6
    [question3] => 5
    [question4] => 6
    [question5] => 7
    [question6] => 8
    [question7] => 5
)

As you can see the two arrays have identical keys, but different values for each key. 如您所见,两个数组具有相同的键,但是每个键的值不同。

I need to find the keys in the second array that have the same values, so [question1] and [question6] both have a value of 8 . 我需要在第二个数组中找到具有相同值的键,因此[question1][question6]的值均为8 And then in the first array I need to add together the values of [question1] and [question6] because they have a like value in the second array. 然后在第一个数组中,我需要将[question1][question6]的值加在一起,因为它们在第二个数组中具有相似的值。 I need to add the first array values together based on matching values in the second array (if that makes any sense) 我需要根据第二个数组中的匹配值将第一个数组值加在一起(如果有任何意义)

Ideally, the output would be another array that would look something like this: 理想情况下,输出将是另一个看起来像这样的数组:

Array
(
    [5] => 115
    [8] => 109
    [6] => 105
    [7] => 58
)

Where the value of the second array becomes the key and the sum of the added values from the first array is the value. 第二个数组的值成为键,第一个数组的相加值之和就是该值。

Now I won't be picky here, so if we can't get it into that exact format then that is okay. 现在我不会在这里挑剔,因此,如果我们无法将其转换为正确的格式,那就可以了。 I just need to be able to add together the values in the first array based on the similar values in the second array. 我只需要能够基于第二个数组中的相似值将第一个数组中的值加在一起。

I hope this makes sense. 我希望这是有道理的。 If it doesn't please comment and I will do my best to explain further. 如果没有,请发表评论,我会尽力进一步解释。

The simplest solution is to iterate over the second array. 最简单的解决方案是遍历第二个数组。 Lookup the key into the first array and if it exists then add the corresponding value from the first array into the result array, indexed by the value from the second array. 在第一个数组中查找键,如果键存在,则将第一个数组中的对应值添加到结果数组中,并以第二个数组中的值索引。

Something like this: 像这样:

$array1 = array(
    'question1' => 69,
    'question2' => 36,
    'question3' => 57,
    'question4' => 69,
    'question5' => 58,
    'question6' => 40,
    'question7' => 58,
);
$array2 = array(
    'question1' => 8,
    'question2' => 6,
    'question3' => 5,
    'question4' => 6,
    'question5' => 7,
    'question6' => 8,
    'question7' => 5,
);

// Compose the desired result here
$result = array();

// Iterate over the second array; its values become keys in the result array
foreach ($array2 as $key => $val) {
    // If this is the first time when this value is reached then a corresponding
    // value does not yet exists in the result array; add it
    if (! isset($result[$val])) {
        $result[$val] = 0;
    }

    // Lookup the key into the first array
    if (isset($array1[$key])) {
        // If it exists then add its value to the results
        $result[$val] += $array1[$key];
    }
}

// That's all
print_r($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM