简体   繁体   中英

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 . 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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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