0

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.

2
  • Have you tried anything? It's a simple iteration over the second array with lookups into the first one. Commented Dec 29, 2015 at 18:13
  • I have tried a few things such as loop through the second array and looping through the first array within the second array loop and looking for similar keys, but I wasn't able to get the results to add together, they just replaced each other. Can you please show an example to how I can add them together rather than replace them as it loops? Commented Dec 29, 2015 at 18:23

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.